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
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
- Scaling
- Constant
- Instructions
- 9–69
- Plugin surface
- 2 hooks
- Called by
- 10
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 117.
Third-party callbacks on 'pre_schedule_event', 'schedule_event' run inside this call, and their cost is not bounded by anything here.
10 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - serializeserialisation
serialize()called directly - optionoption read or write
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9–11 | none |
| always | 16–18 | __() |
!isset($schedules[$recurrence]) | 16 | wp_get_schedules() |
!isset($schedules[$recurrence]) | 23 | wp_get_schedules(), __() |
isset($schedules[$recurrence]) && $pre !== null && $pre !== false | 36 | wp_get_schedules(), apply_filters() |
isset($schedules[$recurrence]) && $pre !== null | 37–40 | wp_get_schedules(), apply_filters(), is_wp_error() |
isset($schedules[$recurrence]) && $pre === null | 39 | wp_get_schedules(), apply_filters(), apply_filters() |
isset($schedules[$recurrence]) && $pre !== null && $pre === false | 42 | wp_get_schedules(), apply_filters(), __(), hook() |
isset($schedules[$recurrence]) && $pre === null | 46 | wp_get_schedules(), apply_filters(), apply_filters(), __() |
isset($schedules[$recurrence]) && $pre === null | 69 | wp_get_schedules(), apply_filters(), apply_filters(), serialize(), md5(), _get_cron_array(), uksort(), _set_cron_array() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 9–69 | 12 | |
| 8.5 | 117 | 9–69 | 12 | |
| 8.4 | 117 | 9–69 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 119 | 11–71 | 12 | |
| 8.2 | 119 | 11–71 | 12 | |
| 8.1 | 119 | 11–71 | 12 | |
| 7.4 | 119 | 11–71 | 12 |
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:
- apply_filters( pre_schedule_event )filterline 287 (+35 into the body)
Filter to override scheduling an event.
- apply_filters( schedule_event )filterline 305 (+53 into the body)
Modify an event before it is scheduled.
Uses · 7
- __()Retrieves the translation of $text.
- wp_get_schedules()Retrieves supported event recurrence schedules.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _get_cron_array()Retrieves cron info array option.
- _set_cron_array()Updates the cron option with the new cron array.
- WP_Error::__construct()Initializes the error.
Used by · 10
- WP_Recovery_Mode::initialize()Initialize recovery mode for the current request.
- WP_Site_Health::maybe_create_scheduled_event()Creates a weekly cron event, if one does not already exist.
- WP_Upgrader::schedule_temp_backup_cleanup()Schedules the cleanup of the temporary backup directory.
- get_default_post_to_edit()Returns default post information to use when populating the "Write Post" form.
- wp_install()Installs the site.
- wp_reschedule_event()Reschedules a recurring event.
- wp_schedule_delete_old_privacy_export_files()Schedules a `WP_Cron` job to delete expired export files.
- wp_schedule_update_checks()Schedules core, theme, and plugin update checks.
- wp_schedule_update_network_counts()Schedules update of the network-wide counts for the current network.
- wp_schedule_update_user_counts()Schedules a recurring recalculation of the total count of users.
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.
Signature, return type and hooks compared across 5 parsed releases.
$wp_error parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.