返回介绍

wp_next_scheduled()

发布于 2017-09-11 12:31:42 字数 3268 浏览 1011 评论 0 收藏 0

wp_next_scheduled( string $hook,  array $args = array() )

Retrieve the next timestamp for an event.


description


参数

$hook

(string) (Required) Action hook to execute when event is run.

$args

(array) (Optional) Arguments to pass to the hook's callback function.

Default value: array()


返回值

(false|int) The Unix timestamp of the next time the scheduled event will occur.


源代码

File: wp-includes/cron.php

function wp_next_scheduled( $hook, $args = array() ) {
	$crons = _get_cron_array();
	$key = md5(serialize($args));
	if ( empty($crons) )
		return false;
	foreach ( $crons as $timestamp => $cron ) {
		if ( isset( $cron[$hook][$key] ) )
			return $timestamp;
	}
	return false;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/cron.php: _get_cron_array()

Used By

  • wp-includes/cron.php: wp_schedule_single_event()
  • wp-includes/update.php: wp_schedule_update_checks()
  • wp-includes/update.php: wp_version_check()
  • wp-includes/post.php: _publish_post_hook()
  • wp-includes/ms-functions.php: wp_schedule_update_network_counts()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by ub3rst4r

    Note the $args parameter! Not specifying the $args parameter in wp_next_scheduled but having $args for wp_schedule_event will cause many events to be scheduled (instead of just one).

    Bad Example:

    
    if ( ! wp_next_scheduled( 'myevent' ) ) { // This will always be false
    	wp_schedule_event( time(), 'daily', 'myevent', array( false ) );
    }
    

    Good Example:

    
    $args = array( false );
    if ( ! wp_next_scheduled( 'myevent', $args ) ) {
    	wp_schedule_event( time(), 'daily', 'myevent', $args );
    }
    
  2. Be careful when using arguments! WordPress doesn’t compare them 1:1 so you have to pay attention what type these are.

    It’s because WP generates a hash out of them: md5( serialize( $args ) )

    So when you have:

    wp_schedule_event( time(), 'daily', 'action_hook', array( 123 ) );

    And use a string because ie. the value was taken from the meta:

    wp_next_scheduled( 'action_hook', array( '123' ) );

    It will return false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文