# wp_opcache_invalidate()

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

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

## Сигнатура

```php
wp_opcache_invalidate( string $filepath, bool $force = false ): bool
```

## Описание

Эту функцию можно безопасно вызывать, не проверяя расширение файла или доступность расширения OPcache.
Возможность инвалидации кэшируется для повышения производительности.

## Параметры

- `$filepath` `string` — обязательный. Путь к файлу, включая расширение, для которого нужно очистить кэш опкодов.
- `$force` `bool` — необязательный, по умолчанию `false`. Инвалидировать, даже если время изменения не новее, чем у файла в кэше.

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

`bool` — $filepath

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

Файл: `wp-admin/includes/file.php:2714`

```php
function wp_opcache_invalidate( $filepath, $force = false ) {
	static $can_invalidate = null;

	/*
	 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value.
	 *
	 * First, check to see if the function is available to call, then if the host has restricted
	 * the ability to run the function to avoid a PHP warning.
	 *
	 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`.
	 *
	 * If the host has this set, check whether the path in `opcache.restrict_api` matches
	 * the beginning of the path of the origin file.
	 *
	 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()`
	 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI.
	 *
	 * For more details, see:
	 * - https://www.php.net/manual/en/opcache.configuration.php
	 * - https://www.php.net/manual/en/reserved.variables.server.php
	 * - https://core.trac.wordpress.org/ticket/36455
	 */
	if ( null === $can_invalidate
		&& function_exists( 'opcache_invalidate' )
		&& ( ! ini_get( 'opcache.restrict_api' )
			|| stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 )
	) {
		$can_invalidate = true;
	}

	// If invalidation is not available, return early.
	if ( ! $can_invalidate ) {
		return false;
	}

	// Verify that file to be invalidated has a PHP extension.
	if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) {
		return false;
	}

	/**
	 * Filters whether to invalidate a file from the opcode cache.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $will_invalidate Whether WordPress will invalidate `$filepath`. Default true.
	 * @param string $filepath        The path to the PHP file to invalidate.
	 */
	if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) {
		return opcache_invalidate( $filepath, $force );
	}

	return false;
}
```

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

- 5.5.0 — Introduced.

## Связанные

Использует: [`stripos`](https://chugunov.pro/api-wordpress/functions/stripos/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: [`wp_opcache_invalidate_directory`](https://chugunov.pro/api-wordpress/functions/wp_opcache_invalidate_directory/), [`wp_edit_theme_plugin_file`](https://chugunov.pro/api-wordpress/functions/wp_edit_theme_plugin_file/), `Core_Upgrader::upgrade`, [`update_core`](https://chugunov.pro/api-wordpress/functions/update_core/), [`copy_dir`](https://chugunov.pro/api-wordpress/functions/copy_dir/).

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