wp_clear_scheduled_hook( string $hook, array $args = array(), bool $wp_error = false )
Unschedules all events attached to the hook with the specified arguments.
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 PHP documentation. Use the ===
operator for testing the return value of this function.
Parameters
- $hook
-
(Required) Action hook, the execution of which will be unscheduled.
- $args
-
(Optional) Array containing each separate argument to pass to the hook's callback function.<br> 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.<br>
Default value: array()
- $wp_error
-
(Optional) Whether to return a WP_Error on failure.
Default value: false
Return
(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.
Source
File: wp-includes/cron.php
function wp_clear_scheduled_hook( $hook, $args = array() ) {
// 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__, 'WP-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 );
}
// 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;
$key = md5( serialize( $args ) );
foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron[ $hook ][ $key ] ) ) {
wp_unschedule_event( $timestamp, $hook, $args );
}
}
}
Changelog
Version | Description |
---|---|
5.7.0 | The $wp_error parameter was added. |
5.1.0 | Return value modified to indicate success or failure, 'pre_clear_scheduled_hook' filter added to short-circuit the function. |
2.1.0 | Introduced. |