Title: wp_opcache_invalidate
Published: August 11, 2020
Last modified: April 28, 2025

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#wp--skip-link--target)

Attempts to clear the opcode cache for an individual PHP file.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#description)󠁿

This function can be called safely without having to check the file extension or
availability of the OPcache extension.

Whether or not invalidation is possible is cached to improve performance.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#parameters)󠁿

 `$filepath`stringrequired

Path to the file, including extension, for which the opcode cache is to be cleared.

`$force`booloptional

Invalidate even if the modification time is not newer than the file in cache.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#return)󠁿

 bool True if opcache was invalidated for `$filepath`, or there was nothing to invalidate.

False if opcache invalidation is not available, or is disabled via filter.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/file.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/file.php#L2715)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/file.php#L2715-L2768)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_opcache_invalidate_file’, bool $will_invalidate, string $filepath )](https://developer.wordpress.org/reference/hooks/wp_opcache_invalidate_file/)

Filters whether to invalidate a file from the opcode cache.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [wp_opcache_invalidate_directory()](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate_directory/)`wp-admin/includes/file.php` |

Attempts to clear the opcode cache for a directory of files.

  | 
| [wp_edit_theme_plugin_file()](https://developer.wordpress.org/reference/functions/wp_edit_theme_plugin_file/)`wp-admin/includes/file.php` |

Attempts to edit a file for a theme or plugin.

  | 
| [Core_Upgrader::upgrade()](https://developer.wordpress.org/reference/classes/core_upgrader/upgrade/)`wp-admin/includes/class-core-upgrader.php` |

Upgrades WordPress core.

  | 
| [update_core()](https://developer.wordpress.org/reference/functions/update_core/)`wp-admin/includes/update-core.php` |

Upgrades the core of WordPress.

  | 
| [copy_dir()](https://developer.wordpress.org/reference/functions/copy_dir/)`wp-admin/includes/file.php` |

Copies a directory from one location to another via the WordPress Filesystem Abstraction.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_opcache_invalidate/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.5.0](https://developer.wordpress.org/reference/since/5.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_opcache_invalidate%2F)
before being able to contribute a note or feedback.