wppaste
WordPress

wp_schedule_event( int $timestamp, string $recurrence, 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:252
Schedules a recurring event.

Description

Schedules a hook which will be triggered by WordPress at the specified interval.
The action will trigger when someone visits your WordPress site if the scheduled time has passed.

Valid values for the recurrence are 'hourly', 'twicedaily', 'daily', and 'weekly'.
These can be extended using the 'cron_schedules' filter in wp_get_schedules().

Use wp_next_scheduled() to prevent duplicate events.

Use wp_schedule_single_event() to schedule a non-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.
$recurrencestring
How often the event should subsequently recur.
See wp_get_schedules() for accepted values.
$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.
These arguments are used to uniquely identify the scheduled event and must match those used when the event was originally scheduled. If the arguments do not match exactly, WordPress will treat the event as different, which can lead to duplicate cron events being scheduled unintentionally, excessive growth of the 'cron' option, and database performance issues.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_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
Moderate

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

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–69

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

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
11

11 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_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 · 10 distinct outcomes

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

WhenInstructionsCalls it makes
always9–11none
always16–18__()
!isset($schedules[$recurrence])16wp_get_schedules()
!isset($schedules[$recurrence])23wp_get_schedules(), __()
isset($schedules[$recurrence]) && $pre !== null && $pre !== false36wp_get_schedules(), apply_filters()
isset($schedules[$recurrence]) && $pre !== null37–40wp_get_schedules(), apply_filters(), is_wp_error()
isset($schedules[$recurrence]) && $pre === null39wp_get_schedules(), apply_filters(), apply_filters()
isset($schedules[$recurrence]) && $pre !== null && $pre === false42wp_get_schedules(), apply_filters(), __(), hook()
isset($schedules[$recurrence]) && $pre === null46wp_get_schedules(), apply_filters(), apply_filters(), __()
isset($schedules[$recurrence]) && $pre === null69wp_get_schedules(), apply_filters(), apply_filters(), serialize(), md5(), _get_cron_array(), uksort(), _set_cron_array()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1179–6912
8.51179–6912
8.41179–69122 fewer instructions than PHP 8.3
8.311911–7112
8.211911–7112
8.111911–7112
7.411911–7112

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_event() runs, in this order:

  1. apply_filters( pre_schedule_event )filterline 287 (+35 into the body)

    Filter to override scheduling an event.

  2. apply_filters( schedule_event )filterline 305 (+53 into the body)

    Modify an event before it is scheduled.

Uses · 7

Used by · 11

Source code

function wp_schedule_event( $timestamp, $recurrence, $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;	} 	$schedules = wp_get_schedules(); 	if ( ! isset( $schedules[ $recurrence ] ) ) {		if ( $wp_error ) {			return new WP_Error(				'invalid_schedule',				__( 'Event schedule does not exist.' )			);		} 		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 */	$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;	} 	/** This filter is documented in wp-includes/cron.php */	$event = apply_filters( 'schedule_event', $event ); 	// A plugin disallowed this event.	if ( ! $event ) {		if ( $wp_error ) {			return new WP_Error(				'schedule_event_false',				__( 'A plugin disallowed this event.' )			);		} 		return false;	} 	$key = md5( serialize( $event->args ) ); 	$crons = _get_cron_array(); 	$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(		'schedule' => $event->schedule,		'args'     => $event->args,		'interval' => $event->interval,	);	uksort( $crons, 'strnatcasecmp' ); 	return _set_cron_array( $crons, $wp_error );}

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 7.1.0 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.