9 * Schedules an event to run only once.
11 * Schedules an event which will execute once by the WordPress actions core at
12 * a time which you specify. The action will fire off when someone visits your
13 * WordPress site, if the schedule time has passed.
15 * Note that scheduling an event to occur within 10 minutes of an existing event
16 * with the same action hook will be ignored unless you pass unique `$args` values
17 * for each scheduled event.
20 * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
22 * @param int $timestamp Unix timestamp (UTC) for when to run the event.
23 * @param string $hook Action hook to execute when event is run.
24 * @param array $args Optional. Arguments to pass to the hook's callback function.
25 * @return false|void False if the event does not get scheduled.
27 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
28 // Make sure timestamp is a positive integer
29 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
33 // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it
34 $next = wp_next_scheduled($hook, $args);
35 if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
39 $crons = _get_cron_array();
40 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
42 * Filters a single event before it is scheduled.
46 * @param stdClass $event {
47 * An object containing an event's data.
49 * @type string $hook Action hook to execute when event is run.
50 * @type int $timestamp Unix timestamp (UTC) for when to run the event.
51 * @type string|false $schedule How often the event should recur. See `wp_get_schedules()`.
52 * @type array $args Arguments to pass to the hook's callback function.
55 $event = apply_filters( 'schedule_event', $event );
57 // A plugin disallowed this event
61 $key = md5(serialize($event->args));
63 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
64 uksort( $crons, "strnatcasecmp" );
65 _set_cron_array( $crons );
69 * Schedule a recurring event.
71 * Schedules a hook which will be executed by the WordPress actions core on a
72 * specific interval, specified by you. The action will trigger when someone
73 * visits your WordPress site, if the scheduled time has passed.
75 * Valid values for the recurrence are hourly, daily, and twicedaily. These can
76 * be extended using the {@see 'cron_schedules'} filter in wp_get_schedules().
78 * Use wp_next_scheduled() to prevent duplicates
82 * @param int $timestamp Unix timestamp (UTC) for when to run the event.
83 * @param string $recurrence How often the event should recur.
84 * @param string $hook Action hook to execute when event is run.
85 * @param array $args Optional. Arguments to pass to the hook's callback function.
86 * @return false|void False if the event does not get scheduled.
88 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
89 // Make sure timestamp is a positive integer
90 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
94 $crons = _get_cron_array();
95 $schedules = wp_get_schedules();
97 if ( !isset( $schedules[$recurrence] ) )
100 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
101 /** This filter is documented in wp-includes/cron.php */
102 $event = apply_filters( 'schedule_event', $event );
104 // A plugin disallowed this event
108 $key = md5(serialize($event->args));
110 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
111 uksort( $crons, "strnatcasecmp" );
112 _set_cron_array( $crons );
116 * Reschedule a recurring event.
120 * @param int $timestamp Unix timestamp (UTC) for when to run the event.
121 * @param string $recurrence How often the event should recur.
122 * @param string $hook Action hook to execute when event is run.
123 * @param array $args Optional. Arguments to pass to the hook's callback function.
124 * @return false|void False if the event does not get rescheduled.
126 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
127 // Make sure timestamp is a positive integer
128 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
132 $crons = _get_cron_array();
133 $schedules = wp_get_schedules();
134 $key = md5( serialize( $args ) );
137 // First we try to get it from the schedule
138 if ( isset( $schedules[ $recurrence ] ) ) {
139 $interval = $schedules[ $recurrence ]['interval'];
141 // Now we try to get it from the saved interval in case the schedule disappears
142 if ( 0 == $interval ) {
143 $interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
145 // Now we assume something is wrong and fail to schedule
146 if ( 0 == $interval ) {
152 if ( $timestamp >= $now ) {
153 $timestamp = $now + $interval;
155 $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
158 wp_schedule_event( $timestamp, $recurrence, $hook, $args );
162 * Unschedule a previously scheduled event.
164 * The $timestamp and $hook parameters are required so that the event can be
169 * @param int $timestamp Unix timestamp (UTC) for when to run the event.
170 * @param string $hook Action hook, the execution of which will be unscheduled.
171 * @param array $args Arguments to pass to the hook's callback function.
172 * Although not passed to a callback function, these arguments are used
173 * to uniquely identify the scheduled event, so they should be the same
174 * as those used when originally scheduling the event.
175 * @return false|void False if the event does not get unscheduled.
177 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
178 // Make sure timestamp is a positive integer
179 if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
183 $crons = _get_cron_array();
184 $key = md5(serialize($args));
185 unset( $crons[$timestamp][$hook][$key] );
186 if ( empty($crons[$timestamp][$hook]) )
187 unset( $crons[$timestamp][$hook] );
188 if ( empty($crons[$timestamp]) )
189 unset( $crons[$timestamp] );
190 _set_cron_array( $crons );
194 * Unschedule all events attached to the specified hook.
198 * @param string $hook Action hook, the execution of which will be unscheduled.
199 * @param array $args Optional. Arguments that were to be passed to the hook's callback function.
201 function wp_clear_scheduled_hook( $hook, $args = array() ) {
202 // Backward compatibility
203 // Previously this function took the arguments as discrete vars rather than an array like the rest of the API
204 if ( !is_array($args) ) {
205 _deprecated_argument( __FUNCTION__, '3.0.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
206 $args = array_slice( func_get_args(), 1 );
209 // This logic duplicates wp_next_scheduled()
210 // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
211 // and, wp_next_scheduled() returns the same schedule in an infinite loop.
212 $crons = _get_cron_array();
213 if ( empty( $crons ) )
216 $key = md5( serialize( $args ) );
217 foreach ( $crons as $timestamp => $cron ) {
218 if ( isset( $cron[ $hook ][ $key ] ) ) {
219 wp_unschedule_event( $timestamp, $hook, $args );
225 * Retrieve the next timestamp for an event.
229 * @param string $hook Action hook to execute when event is run.
230 * @param array $args Optional. Arguments to pass to the hook's callback function.
231 * @return false|int The Unix timestamp of the next time the scheduled event will occur.
233 function wp_next_scheduled( $hook, $args = array() ) {
234 $crons = _get_cron_array();
235 $key = md5(serialize($args));
238 foreach ( $crons as $timestamp => $cron ) {
239 if ( isset( $cron[$hook][$key] ) )
246 * Sends a request to run cron through HTTP request that doesn't halt page loading.
250 * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
252 function spawn_cron( $gmt_time = 0 ) {
254 $gmt_time = microtime( true );
256 if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
260 * Get the cron lock, which is a Unix timestamp of when the last cron was spawned
261 * and has not finished running.
263 * Multiple processes on multiple web servers can run this code concurrently,
264 * this lock attempts to make spawning as atomic as possible.
266 $lock = get_transient('doing_cron');
268 if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
271 // don't run if another process is currently running it or more than once every 60 sec.
272 if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
276 $crons = _get_cron_array();
277 if ( !is_array($crons) )
280 $keys = array_keys( $crons );
281 if ( isset($keys[0]) && $keys[0] > $gmt_time )
284 if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
285 if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
289 $doing_wp_cron = sprintf( '%.22F', $gmt_time );
290 set_transient( 'doing_cron', $doing_wp_cron );
293 wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
296 // flush any buffers and send the headers
297 while ( @ob_end_flush() );
300 WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
304 // Set the cron lock with the current unix timestamp, when the cron is being spawned.
305 $doing_wp_cron = sprintf( '%.22F', $gmt_time );
306 set_transient( 'doing_cron', $doing_wp_cron );
309 * Filters the cron request arguments.
312 * @since 4.5.0 The `$doing_wp_cron` parameter was added.
314 * @param array $cron_request_array {
315 * An array of cron request URL arguments.
317 * @type string $url The cron request URL.
318 * @type int $key The 22 digit GMT microtime.
319 * @type array $args {
320 * An array of cron request arguments.
322 * @type int $timeout The request timeout in seconds. Default .01 seconds.
323 * @type bool $blocking Whether to set blocking for the request. Default false.
324 * @type bool $sslverify Whether SSL should be verified for the request. Default false.
327 * @param string $doing_wp_cron The unix timestamp of the cron lock.
329 $cron_request = apply_filters( 'cron_request', array(
330 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
331 'key' => $doing_wp_cron,
335 /** This filter is documented in wp-includes/class-wp-http-streams.php */
336 'sslverify' => apply_filters( 'https_local_ssl_verify', false )
340 wp_remote_post( $cron_request['url'], $cron_request['args'] );
344 * Run scheduled callbacks or spawn cron for all scheduled events.
349 // Prevent infinite loops caused by lack of wp-cron.php
350 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
353 if ( false === $crons = _get_cron_array() )
356 $gmt_time = microtime( true );
357 $keys = array_keys( $crons );
358 if ( isset($keys[0]) && $keys[0] > $gmt_time )
361 $schedules = wp_get_schedules();
362 foreach ( $crons as $timestamp => $cronhooks ) {
363 if ( $timestamp > $gmt_time ) break;
364 foreach ( (array) $cronhooks as $hook => $args ) {
365 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
367 spawn_cron( $gmt_time );
374 * Retrieve supported event recurrence schedules.
376 * The default supported recurrences are 'hourly', 'twicedaily', and 'daily'. A plugin may
377 * add more by hooking into the {@see 'cron_schedules'} filter. The filter accepts an array
378 * of arrays. The outer array has a key that is the name of the schedule or for
379 * example 'weekly'. The value is an array with two keys, one is 'interval' and
380 * the other is 'display'.
382 * The 'interval' is a number in seconds of when the cron job should run. So for
383 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
384 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
386 * The 'display' is the description. For the 'weekly' key, the 'display' would
387 * be `__( 'Once Weekly' )`.
389 * For your plugin, you will be passed an array. you can easily add your
390 * schedule by doing the following.
392 * // Filter parameter variable name is 'array'.
393 * $array['weekly'] = array(
394 * 'interval' => 604800,
395 * 'display' => __( 'Once Weekly' )
403 function wp_get_schedules() {
405 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),
406 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
407 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),
410 * Filters the non-default cron schedules.
414 * @param array $new_schedules An array of non-default cron schedules. Default empty.
416 return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
420 * Retrieve the recurrence schedule for an event.
422 * @see wp_get_schedules() for available schedules.
426 * @param string $hook Action hook to identify the event.
427 * @param array $args Optional. Arguments passed to the event's callback function.
428 * @return string|false False, if no schedule. Schedule name on success.
430 function wp_get_schedule($hook, $args = array()) {
431 $crons = _get_cron_array();
432 $key = md5(serialize($args));
435 foreach ( $crons as $timestamp => $cron ) {
436 if ( isset( $cron[$hook][$key] ) )
437 return $cron[$hook][$key]['schedule'];
447 * Retrieve cron info array option.
452 * @return false|array CRON info array.
454 function _get_cron_array() {
455 $cron = get_option('cron');
456 if ( ! is_array($cron) )
459 if ( !isset($cron['version']) )
460 $cron = _upgrade_cron_array($cron);
462 unset($cron['version']);
468 * Updates the CRON option with the new CRON array.
473 * @param array $cron Cron info array from _get_cron_array().
475 function _set_cron_array($cron) {
476 $cron['version'] = 2;
477 update_option( 'cron', $cron );
481 * Upgrade a Cron info array.
483 * This function upgrades the Cron info array to version 2.
488 * @param array $cron Cron info array from _get_cron_array().
489 * @return array An upgraded Cron info array.
491 function _upgrade_cron_array($cron) {
492 if ( isset($cron['version']) && 2 == $cron['version'])
497 foreach ( (array) $cron as $timestamp => $hooks) {
498 foreach ( (array) $hooks as $hook => $args ) {
499 $key = md5(serialize($args['args']));
500 $new_cron[$timestamp][$hook][$key] = $args;
504 $new_cron['version'] = 2;
505 update_option( 'cron', $new_cron );