# wp_clear_scheduled_hook()

URL: https://chugunov.pro/api-wordpress/functions/wp_clear_scheduled_hook/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.1.0.

## Сигнатура

```php
wp_clear_scheduled_hook( string $hook, array $args = array(), bool $wp_error = false ): int|false|WP_Error
```

## Описание

Предупреждение: эта функция может вернуть логическое false, но также может вернуть нелогическое значение, приводимое к false. О приведении к логическому типу см. документацию PHP. Для проверки возвращаемого значения этой функции используйте оператор ===.

## Параметры

- `$hook` `string` — обязательный. Хук-действие, выполнение которого будет отменено.
- `$args` `array` — необязательный, по умолчанию `array()`. Массив, содержащий каждый отдельный аргумент для передачи в функцию обратного вызова хука.
  
  Хотя эти аргументы не передаются в функцию обратного вызова, они используются для однозначной идентификации события, поэтому должны совпадать с теми, что использовались при первоначальном планировании события. Если аргументы не совпадают в точности, событие не будет найдено.
- `$wp_error` `bool` — необязательный, по умолчанию `false`. Возвращать ли WP_Error в случае сбоя.

## Возвращаемое значение

`int|false|WP_Error` — WP_Error

## Исходный код

Файл: `wp-includes/cron.php:576`

```php
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;
		}

		return false;
	}

	return count( $results );
}
```

## История изменений

- 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.

## Связанные

Использует: [`_get_cron_array`](https://chugunov.pro/api-wordpress/functions/_get_cron_array/), [`wp_unschedule_event`](https://chugunov.pro/api-wordpress/functions/wp_unschedule_event/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`_deprecated_argument`](https://chugunov.pro/api-wordpress/functions/_deprecated_argument/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`is_wp_error`](https://chugunov.pro/api-wordpress/functions/is_wp_error/), `WP_Error::__construct`.
Используется в: [`populate_network`](https://chugunov.pro/api-wordpress/functions/populate_network/), [`_transition_post_status`](https://chugunov.pro/api-wordpress/functions/_transition_post_status/), [`_future_post_hook`](https://chugunov.pro/api-wordpress/functions/_future_post_hook/), [`check_and_publish_future_post`](https://chugunov.pro/api-wordpress/functions/check_and_publish_future_post/), [`wp_delete_post`](https://chugunov.pro/api-wordpress/functions/wp_delete_post/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_clear_scheduled_hook/
