安排定期活动。
原型
wp_schedule_event( int $timestamp, string $recurrence, string $hook, array $args = array() )
描述
安排一个钩子,该钩子将由你指定的特定间隔由WordPress动作核心执行。如果有人访问你的WordPress网站,如果预定的时间已过,则会触发该操作。
参数
$timestamp
(int)
(Required)
用于何时运行事件的Unix时间戳(UTC)。
$recurrence
(string)
(Required)
该事件应该多久发生一次。
$hook
(string)
(Required)
在事件运行时执行的Action钩子。
$args
(array)
(Optional)
传递给hook的回调函数的参数。
返回值
(false|void)
如果事件未安排,则返回false。
源文件
路径:wp-includes/cron.php
<?php
...
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
// Make sure timestamp is a positive integer
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
return false;
}
$crons = _get_cron_array();
$schedules = wp_get_schedules();
if ( !isset( $schedules[$recurrence] ) )
return false;
$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
/** This filter is documented in wp-includes/cron.php */
$event = apply_filters( 'schedule_event', $event );
// A plugin disallowed this event
if ( ! $event )
return false;
$key = md5(serialize($event->args));
$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
uksort( $crons, "strnatcasecmp" );
_set_cron_array( $crons );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_schedule_event/