在调用wp_schedule_event()之前是否需要检查wp_next_scheduled()?如果是这样,为什么?

时间:2017-02-09 02:35:18

标签: wordpress

几乎每个wp_schedule_event教程都是这样的:

function my_activation() {
    if (! wp_next_scheduled ( 'my_hourly_event' )) {
    wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}

add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
    // do something every hour
}

(来自https://codex.wordpress.org/Function_Reference/wp_schedule_event#Examples

如果您不进行检查,事件是否会被安排,或者"再次安排"或者什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

在运行wp_schedule_event之前检查事件是否已经安排,这是一项预防措施。

事件的调度通常在插件激活时进行。通常还有一个停用钩子来取消安排事件。

如果现有事件未正确清除,则会出现问题。有许多原因可能无法清除事件,例如用户手动删除插件或停用时出错。

重新激活插件会再次使用不同的时间戳添加相同的事件,从而导致回调的执行频率高于预期。