wp_get_scheduled_event( string $hook, array $args = array(), int|null $timestamp = null ): object|false
- Since
- 5.1.0
- Source
wp-includes/cron.php:751
Description
Retrieves the full event object for a given event, if no timestamp is specified the next scheduled event is returned.
Compatibility
- WordPress
- since 5.1.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
$hookstring- Action hook of the event.
$argsarrayoptional- Array containing each separate argument to pass to the hook's callback function.
Although not passed to a callback, these arguments are used to uniquely identify the event, so they should be the same as those used when originally scheduling the event.
Default empty array.Default:array() $timestampint|nulloptional- Unix timestamp (UTC) of the event. If not specified, the next scheduled event is returned. Default null.Default:
null
Return value
object|false- The event object. False if the event does not exist.
$hookstringAction hook to execute when the event is run.$timestampintUnix timestamp (UTC) for when to next run the event.$schedulestring|falseHow often the event should subsequently recur.$argsarrayArray containing each separate argument to pass to the hook's callback function.$intervalintOptional. The interval time in seconds for the schedule. Only present for recurring events.
Performance profile
How much work a call to wp_get_scheduled_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
- Scaling
- Scales with input
- Instructions
- 14–63
- Plugin surface
- 1 hook
- Called by
- 4
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
Third-party callbacks on 'pre_get_scheduled_event' run inside this call, and their cost is not bounded by anything here.
4 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_get_scheduled_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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_scheduled_event() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–18 | apply_filters() |
$pre === null && empty($crons) | 21–23 | apply_filters(), _get_cron_array() |
$pre === null && !empty($crons) | 33–63 | apply_filters(), _get_cron_array(), serialize(), md5() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 72 | 14–63 | 11 | |
| 8.5 | 72 | 14–63 | 11 | |
| 8.4 | 72 | 14–63 | 11 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 74 | 14–65 | 11 | |
| 8.2 | 74 | 14–65 | 11 | |
| 8.1 | 74 | 14–65 | 11 | |
| 7.4 | 74 | 14–65 | 11 |
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 · 1
One hook fires while wp_get_scheduled_event() runs, in this order:
- apply_filters( pre_get_scheduled_event )filterline 770 (+19 into the body)
Filter to override retrieving a scheduled event.
Uses · 2
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _get_cron_array()Retrieves cron info array option.
Used by · 4
- upgrade_640()Executes changes made in WordPress 6.4.0.
- wp_get_schedule()Retrieves the name of the recurrence schedule for an event.
- wp_next_scheduled()Retrieves the next timestamp for an event.
- wp_reschedule_event()Reschedules a recurring event.
Source code
function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { /** * Filter to override retrieving a scheduled event. * * Returning a non-null value will short-circuit the normal process, * returning the filtered value instead. * * Return false if the event does not exist, otherwise an event object * should be returned. * * @since 5.1.0 * * @param null|false|object $pre Value to return instead. Default null to continue retrieving the event. * @param string $hook Action hook of the event. * @param array $args Array containing each separate argument to pass to the hook's callback function. * Although not passed to a callback, these arguments are used to uniquely identify * the event. * @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event. */ $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp ); if ( null !== $pre ) { return $pre; } if ( null !== $timestamp && ! is_numeric( $timestamp ) ) { return false; } $crons = _get_cron_array(); if ( empty( $crons ) ) { return false; } $key = md5( serialize( $args ) ); if ( ! $timestamp ) { // Get next event. $next = false; foreach ( $crons as $timestamp => $cron ) { if ( isset( $cron[ $hook ][ $key ] ) ) { $next = $timestamp; break; } } if ( ! $next ) { return false; } $timestamp = $next; } elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) { return false; } $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $crons[ $timestamp ][ $hook ][ $key ]['schedule'], 'args' => $args, ); if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) { $event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval']; } return $event;}Changelog
Introduced in 5.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
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.