wp_unschedule_hook()
Unschedules all events attached to the hook.
Can be useful for plugins when deactivating to clean up the cron queue.
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.
Uses: _set_cron_array()
Hooks from the function
Return
Int|false|WP_Error
. On success an integer indicating number of events unscheduled (0 indicates no events were registered on the hook), false or WP_Error if unscheduling fails.
Usage
wp_unschedule_hook( $hook, $wp_error );
- $hook(string) (required)
- Action hook, the execution of which will be unscheduled.
- $wp_error(true|false)
- Whether to return a WP_Error on failure.
Default: false
Examples
#1 Remove (Cancel) all Cron events of the CRON hook
wp_unschedule_hook( 'my_hourly_event' );
#2 Cancel all cron events when the plugin is deactivated
// when deactivating the plugin, cancel the previously created task register_deactivation_hook( __FILE__, 'my_deactivation'); // add a task when the plugin is activated register_activation_hook( __FILE__, 'my_activation' ); function my_deactivation() { wp_unschedule_hook( 'my_hourly_event' ); } function my_activation() { // Delete all the same cron tasks, just in case, to add new ones from a "clean slate". // This may be necessary if the same task was added incorrectly before // (without checking if it already exists) wp_unschedule_hook( 'my_hourly_event' ); // add a new cron task wp_schedule_event( time(), 'hourly', 'my_hourly_event'); }
Changelog
Since 4.9.0 | Introduced. |
Since 5.1.0 | Return value added to indicate success or failure. |
Since 5.7.0 | The $wp_error parameter was added. |