# wp_unschedule_hook()

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

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

## Сигнатура

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

## Описание

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

## Параметры

- `$hook` `string` — обязательный. Хук-действие, выполнение которого будет отменено.
- `$wp_error` `bool` — необязательный, по умолчанию `false`. Возвращать ли WP_Error в случае сбоя.

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

`int|false|WP_Error` — WP_Error

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

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

```php
function wp_unschedule_hook( $hook, $wp_error = false ) {
	/**
	 * Filter to override clearing all events attached to the 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). If unscheduling
	 * one or more events fails then return either a WP_Error object or false depending
	 * on the value of the `$wp_error` parameter.
	 *
	 * @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 hook.
	 * @param string                  $hook     Action hook, the execution of which will be unscheduled.
	 * @param bool                    $wp_error Whether to return a WP_Error on failure.
	 */
	$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );

	if ( null !== $pre ) {
		if ( $wp_error && false === $pre ) {
			return new WP_Error(
				'pre_unschedule_hook_false',
				__( 'A plugin prevented the hook from being cleared.' )
			);
		}

		if ( ! $wp_error && is_wp_error( $pre ) ) {
			return false;
		}

		return $pre;
	}

	$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;
	}

	$set = _set_cron_array( $crons, $wp_error );

	if ( true === $set ) {
		return array_sum( $results );
	}

	return $set;
}
```

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

- 5.7.0 — The $wp_error parameter was added.
- 5.1.0 — Return value added to indicate success or failure.
- 4.9.0 — Introduced.

## Связанные

Использует: [`_get_cron_array`](https://chugunov.pro/api-wordpress/functions/_get_cron_array/), [`_set_cron_array`](https://chugunov.pro/api-wordpress/functions/_set_cron_array/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`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`.
Используется в: [`wp_install`](https://chugunov.pro/api-wordpress/functions/wp_install/).

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