A WordPress-centric search engine for devs and theme authors



wp_unschedule_hook ›

Since4.9.0
Deprecatedn/a
wp_unschedule_hook ( $hook )
Parameters:
  • (string) $hook Action hook, the execution of which will be unscheduled.
    Required: Yes
Returns:
  • (bool|int) On success an integer indicating number of events unscheduled (0 indicates no events were registered on the hook), false if unscheduling fails.
Defined at:
Codex:
Change Log:
  • 5.0.0

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 {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use the === operator for testing the return value of this function.



Source

function wp_unschedule_hook( $hook ) {
	$crons = _get_cron_array();
	if ( empty( $crons ) ) {
		return 0;
	}

	$results = array();
	foreach ( $crons as $timestamp => $args ) {
		if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
			$results[] = count( $crons[ $timestamp ][ $hook ] );
		}
		unset( $crons[ $timestamp ][ $hook ] );

		if ( empty( $crons[ $timestamp ] ) ) {
			unset( $crons[ $timestamp ] );
		}
	}

	/*
	 * If the results are empty (zero events to unschedule), no attempt
	 * to update the cron array is required.
	 */
	if ( empty( $results ) ) {
		return 0;
	}
	if ( _set_cron_array( $crons ) ) {
		return array_sum( $results );
	}
	return false;
}