wp_unschedule_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:462
Description
The $timestamp and $hook parameters are required so that the event can be identified.
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) of the event.
$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() $wp_errorbooloptional- Whether to return a WP_Error on failure. Default false.Default:
false
Return value
bool|WP_Error- True if event successfully unscheduled. False or WP_Error on failure.
Performance profile
How much work a call to wp_unschedule_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
- 8–45
- Plugin surface
- 1 hook
- Called by
- 1
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 73.
Third-party callbacks on 'pre_unschedule_event' run inside this call, and their cost is not bounded by anything here.
1 place 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_unschedule_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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_unschedule_event() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–10 | none |
| always | 15–17 | __() |
$pre !== null && $pre !== false | 24 | apply_filters() |
$pre !== null | 25–28 | apply_filters(), is_wp_error() |
$pre !== null && $pre === false | 30 | apply_filters(), __() |
$pre === null | 42–45 | apply_filters(), _get_cron_array(), serialize(), md5(), _set_cron_array() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 73 | 8–45 | 10 | |
| 8.5 | 73 | 8–45 | 10 | |
| 8.4 | 73 | 8–45 | 10 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 75 | 10–47 | 10 | |
| 8.2 | 75 | 10–47 | 10 | |
| 8.1 | 75 | 10–47 | 10 | |
| 7.4 | 75 | 10–47 | 10 |
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_unschedule_event() runs, in this order:
- apply_filters( pre_unschedule_event )filterline 493 (+31 into the body)
Filter to override unscheduling of events.
Uses · 6
- __()Retrieves the translation of $text.
- 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 · 1
- wp_clear_scheduled_hook()Unschedules all events attached to the hook with the specified arguments.
Source code
function wp_unschedule_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; } /** * Filter to override unscheduling of events. * * Returning a non-null value will short-circuit the normal unscheduling * process, causing the function to return the filtered value instead. * * For plugins replacing wp-cron, return true if the event was successfully * unscheduled, 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 $pre Value to return instead. Default null to continue unscheduling the event. * @param int $timestamp Unix timestamp (UTC) for when to run the event. * @param string $hook Action hook, the execution of which will be unscheduled. * @param array $args Arguments to pass to the hook's callback function. * @param bool $wp_error Whether to return a WP_Error on failure. */ $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error ); if ( null !== $pre ) { if ( $wp_error && false === $pre ) { return new WP_Error( 'pre_unschedule_event_false', __( 'A plugin prevented the event from being unscheduled.' ) ); } if ( ! $wp_error && is_wp_error( $pre ) ) { return false; } return $pre; } $crons = _get_cron_array(); $key = md5( serialize( $args ) ); unset( $crons[ $timestamp ][ $hook ][ $key ] ); if ( empty( $crons[ $timestamp ][ $hook ] ) ) { unset( $crons[ $timestamp ][ $hook ] ); } if ( empty( $crons[ $timestamp ] ) ) { unset( $crons[ $timestamp ] ); } 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 6.9.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.