wp_schedule_single_event

Schedules an event to run only once.

Description

(false|void) wp_schedule_single_event( (int) $timestamp, (string) $hook, (array) $args = array() ); 

Schedules an event which will execute once by the WordPress actions core at a time which you specify. The action will fire off when someone visits your WordPress site, if the schedule time has passed.

Note that scheduling an event to occur within 10 minutes of an existing event with the same action hook will be ignored unless you pass unique $args values for each scheduled event.

Returns (false|void)

False if the event does not get scheduled.

Parameters (3)

0. $timestamp (int)
Unix timestamp (UTC) for when to run the event.
1. $hook (string)
Action hook to execute when event is run.
2. $args — Optional. (array) => array()
Arguments to pass to the hook's callback function.

Usage

  1. if ( !function_exists( 'wp_schedule_single_event' ) ) { 
  2. require_once ABSPATH . WPINC . '/cron.php'; 
  3.  
  4. // Unix timestamp (UTC) for when to run the event. 
  5. $timestamp = -1; 
  6.  
  7. // Action hook to execute when event is run. 
  8. $hook = ''; 
  9.  
  10. // Optional. Arguments to pass to the hook's callback function. 
  11. $args = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_schedule_single_event($timestamp, $hook, $args); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/cron.php  
  1. function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
  2. // Make sure timestamp is a positive integer 
  3. if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) { 
  4. return false; 
  5.  
  6. // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it 
  7. $next = wp_next_scheduled($hook, $args); 
  8. if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) { 
  9. return false; 
  10.  
  11. $crons = _get_cron_array(); 
  12. $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args ); 
  13. /** 
  14. * Filters a single event before it is scheduled. 
  15. * @since 3.1.0 
  16. * @param stdClass $event { 
  17. * An object containing an event's data. 
  18. * @type string $hook Action hook to execute when event is run. 
  19. * @type int $timestamp Unix timestamp (UTC) for when to run the event. 
  20. * @type string|false $schedule How often the event should recur. See `wp_get_schedules()`. 
  21. * @type array $args Arguments to pass to the hook's callback function. 
  22. * } 
  23. */ 
  24. $event = apply_filters( 'schedule_event', $event ); 
  25.  
  26. // A plugin disallowed this event 
  27. if ( ! $event ) 
  28. return false; 
  29.  
  30. $key = md5(serialize($event->args)); 
  31.  
  32. $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); 
  33. uksort( $crons, "strnatcasecmp" ); 
  34. _set_cron_array( $crons );