9 * Schedules a hook to run only once.
11 * Schedules a hook which will be executed 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.
16 * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
18 * @param int $timestamp Timestamp for when to run the event.
19 * @param string $hook Action hook to execute when cron is run.
20 * @param array $args Optional. Arguments to pass to the hook's callback function.
22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
23 // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
24 $next = wp_next_scheduled($hook, $args);
25 if ( $next && $next <= $timestamp + 10 * MINUTE_IN_SECONDS )
28 $crons = _get_cron_array();
29 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
30 $event = apply_filters('schedule_event', $event);
32 // A plugin disallowed this event
36 $key = md5(serialize($event->args));
38 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
39 uksort( $crons, "strnatcasecmp" );
40 _set_cron_array( $crons );
44 * Schedule a periodic event.
46 * Schedules a hook which will be executed by the WordPress actions core on a
47 * specific interval, specified by you. The action will trigger when someone
48 * visits your WordPress site, if the scheduled time has passed.
50 * Valid values for the recurrence are hourly, daily and twicedaily. These can
51 * be extended using the cron_schedules filter in wp_get_schedules().
53 * Use wp_next_scheduled() to prevent duplicates
57 * @param int $timestamp Timestamp for when to run the event.
58 * @param string $recurrence How often the event should recur.
59 * @param string $hook Action hook to execute when cron is run.
60 * @param array $args Optional. Arguments to pass to the hook's callback function.
61 * @return bool|null False on failure, null when complete with scheduling event.
63 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
64 $crons = _get_cron_array();
65 $schedules = wp_get_schedules();
67 if ( !isset( $schedules[$recurrence] ) )
70 $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
71 $event = apply_filters('schedule_event', $event);
73 // A plugin disallowed this event
77 $key = md5(serialize($event->args));
79 $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
80 uksort( $crons, "strnatcasecmp" );
81 _set_cron_array( $crons );
85 * Reschedule a recurring event.
89 * @param int $timestamp Timestamp for when to run the event.
90 * @param string $recurrence How often the event should recur.
91 * @param string $hook Action hook to execute when cron is run.
92 * @param array $args Optional. Arguments to pass to the hook's callback function.
93 * @return bool|null False on failure. Null when event is rescheduled.
95 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
96 $crons = _get_cron_array();
97 $schedules = wp_get_schedules();
98 $key = md5(serialize($args));
101 // First we try to get it from the schedule
102 if ( 0 == $interval )
103 $interval = $schedules[$recurrence]['interval'];
104 // Now we try to get it from the saved interval in case the schedule disappears
105 if ( 0 == $interval )
106 $interval = $crons[$timestamp][$hook][$key]['interval'];
107 // Now we assume something is wrong and fail to schedule
108 if ( 0 == $interval )
113 if ( $timestamp >= $now )
114 $timestamp = $now + $interval;
116 $timestamp = $now + ($interval - (($now - $timestamp) % $interval));
118 wp_schedule_event( $timestamp, $recurrence, $hook, $args );
122 * Unschedule a previously scheduled cron job.
124 * The $timestamp and $hook parameters are required, so that the event can be
129 * @param int $timestamp Timestamp for when to run the event.
130 * @param string $hook Action hook, the execution of which will be unscheduled.
131 * @param array $args Arguments to pass to the hook's callback function.
132 * Although not passed to a callback function, these arguments are used
133 * to uniquely identify the scheduled event, so they should be the same
134 * as those used when originally scheduling the event.
136 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
137 $crons = _get_cron_array();
138 $key = md5(serialize($args));
139 unset( $crons[$timestamp][$hook][$key] );
140 if ( empty($crons[$timestamp][$hook]) )
141 unset( $crons[$timestamp][$hook] );
142 if ( empty($crons[$timestamp]) )
143 unset( $crons[$timestamp] );
144 _set_cron_array( $crons );
148 * Unschedule all cron jobs attached to a specific hook.
152 * @param string $hook Action hook, the execution of which will be unscheduled.
153 * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
155 function wp_clear_scheduled_hook( $hook, $args = array() ) {
156 // Backward compatibility
157 // Previously this function took the arguments as discrete vars rather than an array like the rest of the API
158 if ( !is_array($args) ) {
159 _deprecated_argument( __FUNCTION__, '3.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
160 $args = array_slice( func_get_args(), 1 );
163 // This logic duplicates wp_next_scheduled()
164 // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
165 // and, wp_next_scheduled() returns the same schedule in an infinite loop.
166 $crons = _get_cron_array();
167 if ( empty( $crons ) )
170 $key = md5( serialize( $args ) );
171 foreach ( $crons as $timestamp => $cron ) {
172 if ( isset( $cron[ $hook ][ $key ] ) ) {
173 wp_unschedule_event( $timestamp, $hook, $args );
179 * Retrieve the next timestamp for a cron event.
183 * @param string $hook Action hook to execute when cron is run.
184 * @param array $args Optional. Arguments to pass to the hook's callback function.
185 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
187 function wp_next_scheduled( $hook, $args = array() ) {
188 $crons = _get_cron_array();
189 $key = md5(serialize($args));
192 foreach ( $crons as $timestamp => $cron ) {
193 if ( isset( $cron[$hook][$key] ) )
200 * Send request to run cron through HTTP request that doesn't halt page loading.
204 * @return null Cron could not be spawned, because it is not needed to run.
206 function spawn_cron( $gmt_time = 0 ) {
209 $gmt_time = microtime( true );
211 if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
215 * multiple processes on multiple web servers can run this code concurrently
216 * try to make this as atomic as possible by setting doing_cron switch
218 $lock = get_transient('doing_cron');
220 if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
223 // don't run if another process is currently running it or more than once every 60 sec.
224 if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
228 $crons = _get_cron_array();
229 if ( !is_array($crons) )
232 $keys = array_keys( $crons );
233 if ( isset($keys[0]) && $keys[0] > $gmt_time )
236 if ( defined('ALTERNATE_WP_CRON') && ALTERNATE_WP_CRON ) {
237 if ( !empty($_POST) || defined('DOING_AJAX') )
240 $doing_wp_cron = sprintf( '%.22F', $gmt_time );
241 set_transient( 'doing_cron', $doing_wp_cron );
244 wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
247 // flush any buffers and send the headers
248 while ( @ob_end_flush() );
251 WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
255 $doing_wp_cron = sprintf( '%.22F', $gmt_time );
256 set_transient( 'doing_cron', $doing_wp_cron );
258 $cron_request = apply_filters( 'cron_request', array(
259 'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ),
260 'key' => $doing_wp_cron,
261 'args' => array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) )
264 wp_remote_post( $cron_request['url'], $cron_request['args'] );
268 * Run scheduled callbacks or spawn cron for all scheduled events.
272 * @return null When doesn't need to run Cron.
276 // Prevent infinite loops caused by lack of wp-cron.php
277 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
280 if ( false === $crons = _get_cron_array() )
283 $gmt_time = microtime( true );
284 $keys = array_keys( $crons );
285 if ( isset($keys[0]) && $keys[0] > $gmt_time )
288 $schedules = wp_get_schedules();
289 foreach ( $crons as $timestamp => $cronhooks ) {
290 if ( $timestamp > $gmt_time ) break;
291 foreach ( (array) $cronhooks as $hook => $args ) {
292 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
294 spawn_cron( $gmt_time );
301 * Retrieve supported and filtered Cron recurrences.
303 * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
304 * hooking into the 'cron_schedules' filter. The filter accepts an array of
305 * arrays. The outer array has a key that is the name of the schedule or for
306 * example 'weekly'. The value is an array with two keys, one is 'interval' and
307 * the other is 'display'.
309 * The 'interval' is a number in seconds of when the cron job should run. So for
310 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
311 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
313 * The 'display' is the description. For the 'weekly' key, the 'display' would
314 * be <code>__('Once Weekly')</code>.
316 * For your plugin, you will be passed an array. you can easily add your
317 * schedule by doing the following.
319 * // filter parameter variable name is 'array'
320 * $array['weekly'] = array(
321 * 'interval' => 604800,
322 * 'display' => __('Once Weekly')
330 function wp_get_schedules() {
332 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ) ),
333 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
334 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ) ),
336 return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
340 * Retrieve Cron schedule for hook with arguments.
344 * @param string $hook Action hook to execute when cron is run.
345 * @param array $args Optional. Arguments to pass to the hook's callback function.
346 * @return string|bool False, if no schedule. Schedule on success.
348 function wp_get_schedule($hook, $args = array()) {
349 $crons = _get_cron_array();
350 $key = md5(serialize($args));
353 foreach ( $crons as $timestamp => $cron ) {
354 if ( isset( $cron[$hook][$key] ) )
355 return $cron[$hook][$key]['schedule'];
365 * Retrieve cron info array option.
370 * @return array CRON info array.
372 function _get_cron_array() {
373 $cron = get_option('cron');
374 if ( ! is_array($cron) )
377 if ( !isset($cron['version']) )
378 $cron = _upgrade_cron_array($cron);
380 unset($cron['version']);
386 * Updates the CRON option with the new CRON array.
391 * @param array $cron Cron info array from {@link _get_cron_array()}.
393 function _set_cron_array($cron) {
394 $cron['version'] = 2;
395 update_option( 'cron', $cron );
399 * Upgrade a Cron info array.
401 * This function upgrades the Cron info array to version 2.
406 * @param array $cron Cron info array from {@link _get_cron_array()}.
407 * @return array An upgraded Cron info array.
409 function _upgrade_cron_array($cron) {
410 if ( isset($cron['version']) && 2 == $cron['version'])
415 foreach ( (array) $cron as $timestamp => $hooks) {
416 foreach ( (array) $hooks as $hook => $args ) {
417 $key = md5(serialize($args['args']));
418 $new_cron[$timestamp][$hook][$key] = $args;
422 $new_cron['version'] = 2;
423 update_option( 'cron', $new_cron );