wp_clear_scheduled_hook( string $hook, array $args = array(), bool $wp_error = false ): int|false|WP_Error
- Since
- 2.1.0, 5.1.0, 5.7.0
- Source
wp-includes/cron.php:576
Description
Warning: This function may return boolean false, but may also return a non-boolean value which evaluates to false. For information about casting to booleans see the https://www.php.net/manual/en/language.types.boolean.php PHP documentation. Use the === operator for testing the return value of this function.
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
$hookstring- Action hook, the execution of which will be unscheduled.
$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 must match those used when originally scheduling the event. If the arguments do not match exactly, the event will not be found. Default empty array.Default:array() $wp_errorbooloptional- Whether to return a WP_Error on failure. Default false.Default:
false
Return value
int|false|WP_Error- On success an integer indicating number of events unscheduled (0 indicates no events were registered with the hook and arguments combination), false or WP_Error if unscheduling one or more events fail.
Performance profile
How much work a call to wp_clear_scheduled_hook() 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
- 20–59
- Plugin surface
- 1 hook
- Called by
- 9
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 94.
Third-party callbacks on 'pre_clear_scheduled_hook' run inside this call, and their cost is not bounded by anything here.
9 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_clear_scheduled_hook()
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_clear_scheduled_hook() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($args) && $pre !== null && $pre !== false | 20 | apply_filters() |
is_array($args) && $pre === null && empty($crons) | 21 | apply_filters(), _get_cron_array() |
is_array($args) && $pre !== null | 21–24 | apply_filters(), is_wp_error() |
is_array($args) && $pre !== null && $pre === false | 26 | apply_filters(), __() |
!is_array($args) && $pre !== null && $pre !== false | 31 | __(), _deprecated_argument(), apply_filters() |
!is_array($args) && $pre === null && empty($crons) | 32 | __(), _deprecated_argument(), apply_filters(), _get_cron_array() |
!is_array($args) && $pre !== null | 32–35 | __(), _deprecated_argument(), apply_filters(), is_wp_error() |
!is_array($args) && $pre !== null && $pre === false | 37 | __(), _deprecated_argument(), apply_filters(), __() |
is_array($args) && $pre === null && !empty($crons) | 41–48 | apply_filters(), _get_cron_array(), serialize(), md5(), array_filter() |
!is_array($args) && $pre === null && !empty($crons) | 52–59 | __(), _deprecated_argument(), apply_filters(), _get_cron_array(), serialize(), md5(), array_filter() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 94 instructions, 20–59 executed per call, 12 branches. The work does not change between versions.
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_clear_scheduled_hook() runs, in this order:
- apply_filters( pre_clear_scheduled_hook )filterline 610 (+34 into the body)
Filter to override clearing a scheduled hook.
Uses · 7
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()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.
- wp_unschedule_event()Unschedules a previously scheduled event.
- WP_Error::__construct()Initializes the error.
Used by · 9
- _future_post_hook()Hook used to schedule publication for a post marked for the future.
- _transition_post_status()Hook for managing future post transitions to published.
- check_and_publish_future_post()Publishes future post and make sure post ID has future post status.
- populate_network()Populate network settings.
- upgrade_370()Execute changes made in WordPress 3.7.
- upgrade_372()Execute changes made in WordPress 3.7.2.
- upgrade_450()Executes changes made in WordPress 4.5.0.
- upgrade_640()Executes changes made in WordPress 6.4.0.
- wp_delete_post()Trashes or deletes a post or page.
Source code
function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) { /* * Backward compatibility. * Previously, this function took the arguments as discrete vars rather than an array like the rest of the API. */ if ( ! is_array( $args ) ) { _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) ); $args = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection $wp_error = false; } /** * Filter to override clearing a scheduled hook. * * 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 the number of events successfully * unscheduled (zero if no events were registered with the hook) or false * or a WP_Error if unscheduling one or more events fails. * * @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|int|false|WP_Error $pre Value to return instead. Default null to continue unscheduling 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_clear_scheduled_hook', null, $hook, $args, $wp_error ); if ( null !== $pre ) { if ( $wp_error && false === $pre ) { return new WP_Error( 'pre_clear_scheduled_hook_false', __( 'A plugin prevented the hook from being cleared.' ) ); } if ( ! $wp_error && is_wp_error( $pre ) ) { return false; } return $pre; } /* * This logic duplicates wp_next_scheduled(). * It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing, * and, wp_next_scheduled() returns the same schedule in an infinite loop. */ $crons = _get_cron_array(); if ( empty( $crons ) ) { return 0; } $results = array(); $key = md5( serialize( $args ) ); foreach ( $crons as $timestamp => $cron ) { if ( isset( $cron[ $hook ][ $key ] ) ) { $results[] = wp_unschedule_event( $timestamp, $hook, $args, true ); } } $errors = array_filter( $results, 'is_wp_error' ); $error = new WP_Error(); if ( $errors ) { if ( $wp_error ) { array_walk( $errors, array( $error, 'merge_from' ) ); return $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.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.