wppaste
WordPress

wp_schedule_single_event( int $timestamp, string $hook, array $args = array(), bool $wp_error = false ): bool|WP_Error

Since
2.1.0, 5.1.0, 5.7.0
Source
wp-includes/cron.php:39
Schedules an event to run only once.

Description

Schedules a hook which will be triggered by WordPress at the specified UTC time.
The action will trigger when someone visits your WordPress site if the scheduled 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.

Use wp_next_scheduled() to prevent duplicate events.

Use wp_schedule_event() to schedule a recurring event.

Compatibility

WordPress
since 5.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$timestampint
Unix timestamp (UTC) for when to next run the event.
$hookstring
Action hook to execute when the event is run.
$argsarrayoptional
Array containing arguments to pass to the hook's callback function. Each value in the array is passed to the callback as an individual parameter.
The array keys are ignored. Default empty array.Default: array()
$wp_errorbooloptional
Whether to return a WP_Error on failure. Default false.Default: false

Return value

bool|WP_Error
True if event successfully scheduled. False or WP_Error on failure.

Performance profile

How much work a call to wp_schedule_single_event() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–96

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 150.

Plugin surface
2 hooks

Third-party callbacks on 'pre_schedule_event', 'schedule_event' run inside this call, and their cost is not bounded by anything here.

Called by
15

15 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationserialize()called directly
  • optionoption read or writeget_option()one call below wp_schedule_single_event()

Further down the call graph this can also reach cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_schedule_single_event() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–10none
always15–17__()
$pre !== null && $pre !== false27apply_filters()
$pre !== null28–31apply_filters(), is_wp_error()
$pre !== null && $pre === false33apply_filters(), __(), hook()
$pre === null58–71apply_filters(), _get_cron_array(), serialize(), md5(), time(), time()
$pre === null60–73apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), time()
$pre === null64–77apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), apply_filters()
$pre === null65–78apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), __()
$pre === null66–79apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), time(), apply_filters()
$pre === null67–80apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), time(), __()
$pre === null71–84apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), apply_filters(), __()
3 further outcomes, up to 96 instructions
$pre === null73–86apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), time(), apply_filters(), __()
$pre === null81–94apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), apply_filters(), uksort(), _set_cron_array()
$pre === null83–96apply_filters(), _get_cron_array(), serialize(), md5(), time(), time(), time(), apply_filters(), uksort(), _set_cron_array()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1508–9619
8.51508–9619
8.41508–96192 fewer instructions than PHP 8.3
8.315210–9819
8.215210–9819
8.115210–9819
7.415210–9819

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_schedule_single_event() runs, in this order:

  1. apply_filters( pre_schedule_event )filterline 91 (+52 into the body)

    Filter to override scheduling an event.

  2. apply_filters( schedule_event )filterline 179 (+140 into the body)

    Modify an event before it is scheduled.

Uses · 6

Used by · 15

Show all 15

Source code

function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {	// Make sure timestamp is a positive integer.	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {		if ( $wp_error ) {			return new WP_Error(				'invalid_timestamp',				__( 'Event timestamp must be a valid Unix timestamp.' )			);		} 		return false;	} 	$event = (object) array(		'hook'      => $hook,		'timestamp' => $timestamp,		'schedule'  => false,		'args'      => $args,	); 	/**	 * Filter to override scheduling an event.	 *	 * Returning a non-null value will short-circuit adding the event to the	 * cron array, causing the function to return the filtered value instead.	 *	 * Both single events and recurring events are passed through this filter;	 * single events have `$event->schedule` as false, whereas recurring events	 * have this set to a recurrence from wp_get_schedules(). Recurring	 * events also have the integer recurrence interval set as `$event->interval`.	 *	 * For plugins replacing wp-cron, it is recommended you check for an	 * identical event within ten minutes and apply the {@see 'schedule_event'}	 * filter to check if another plugin has disallowed the event before scheduling.	 *	 * Return true if the event was scheduled, false or a WP_Error if not.	 *	 * @since 5.1.0	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.	 *	 * @param null|bool|WP_Error $result   The value to return instead. Default null to continue adding the event.	 * @param object             $event    {	 *     An object containing an event's data.	 *	 *     @type string       $hook      Action hook to execute when the event is run.	 *     @type int          $timestamp Unix timestamp (UTC) for when to next run the event.	 *     @type string|false $schedule  How often the event should subsequently recur.	 *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.	 *     @type int          $interval  Optional. The interval time in seconds for the schedule. Only present for recurring events.	 * }	 * @param bool               $wp_error Whether to return a WP_Error on failure.	 */	$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error ); 	if ( null !== $pre ) {		if ( $wp_error && false === $pre ) {			return new WP_Error(				'pre_schedule_event_false',				__( 'A plugin prevented the event from being scheduled.' )			);		} 		if ( ! $wp_error && is_wp_error( $pre ) ) {			return false;		} 		return $pre;	} 	/*	 * Check for a duplicated event.	 *	 * Don't schedule an event if there's already an identical event	 * within 10 minutes.	 *	 * When scheduling events within ten minutes of the current time,	 * all past identical events are considered duplicates.	 *	 * When scheduling an event with a past timestamp (ie, before the	 * current time) all events scheduled within the next ten minutes

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.7.0
The $wp_error parameter was added.from the docblock
5.1.0
Return value modified to boolean indicating success or failure, 'pre_schedule_event' filter added to short-circuit the function.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/cron.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.