^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数wp_next_scheduled()用法示例

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

Retrieve the next timestamp for an event.


描述


参数

$hook

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

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

$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;
}

更新日志

Version 描述
2.1.0 Introduced.

相关函数

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()

  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.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈