返回介绍

wp_get_schedule()

发布于 2017-09-11 12:08:29 字数 3424 浏览 1077 评论 0 收藏 0

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

Retrieve the recurrence schedule for an event.


description


参数

$hook

(string) (Required) Action hook to identify the event.

$args

(array) (Optional) Arguments passed to the event's callback function.

Default value: array()


返回值

(string|false) False, if no schedule. Schedule name on success.


源代码

File: wp-includes/cron.php

function wp_get_schedule($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 $cron[$hook][$key]['schedule'];
	}
	return false;
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

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

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 Andrei G

    I suggest that maybe the documentation here should be updated to say that the function returns false if no RECURRENCE.

    I came across an issue lately with a plugin that was scheduling a single event and using wp_get_schedule() to check for the existence of a schedule.

    
    		if ( !wp_get_schedule( 'groups_file_access_session_delete_transients' )) {
    			
    			wp_schedule_single_event( time() + self::SCHEDULE, 'groups_file_access_session_delete_transients' );
    		}

    It took me a while to understand that wp_get_schedule() does not return the actual timestamp like wp_next_scheduled(), but the recurrence value, if any (hourly, daily, etc.).

    As such, in the example above, the scheduling of single events was happening on every single call, causing for the cron field in the database to become gigantic.

    Since wp_get_schedule() seems to not see single events (since they have no recurrence) and will always return false for single events, it is somewhat confusing.

    I suspect people would expect for wp_get_schedule() to work like wp_next_scheduled(), but that’s not the case.

  2. Basic Examples

    
    // If you previously added for example:
    // wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event' );
    
    $schedule = wp_get_schedule( 'my_hourly_event' );
    
    // $schedule == 'hourly'
    
    // Or this if you created something like this:
    // wp_schedule_single_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event', array( 'some_arg' ) );
    
    $schedule = wp_get_schedule( 'my_hourly_event', array( 'some_arg' ) );
    
    // $schedule == 'hourly'
    

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

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

发布评论

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