wp_opcache_invalidate( string $filepath, bool $force = false ): bool
- Since
- 5.5.0
- Source
wp-admin/includes/file.php:2725
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.
Compatibility
- WordPress
- since 5.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filepathstring- Path to the file, including extension, for which the opcode cache is to be cleared.
$forcebooloptional- Invalidate even if the modification time is not newer than the file in cache.
Default false.Default:false
Return value
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.
Performance profile
How much work a call to wp_opcache_invalidate() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 7–24
- Plugin surface
- 1 hook
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 27.
Third-party callbacks on 'wp_opcache_invalidate_file' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_opcache_invalidate() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–8 | none |
| always | 13–14 | strtolower() |
!apply_filters() | 19–20 | strtolower(), apply_filters() |
apply_filters() | 23–24 | strtolower(), apply_filters(), opcache_invalidate() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 27 | 7–24 | 4 | |
| 8.5 | 27 | 7–24 | 4 | |
| 8.4 | 27 | 7–24 | 4 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 30 | 7–27 | 4 | |
| 8.2 | 30 | 7–27 | 4 | |
| 8.1 | 30 | 7–27 | 4 | |
| 7.4 | 30 | 7–27 | 4 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_opcache_invalidate() runs, in this order:
- apply_filters( wp_opcache_invalidate_file )filterline 2773 (+48 into the body)
Filters whether to invalidate a file from the opcode cache.
Uses · 2
- stripos()
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 5
- Core_Upgrader::upgrade()Upgrades WordPress core.
- copy_dir()Copies a directory from one location to another via the WordPress Filesystem Abstraction.
- update_core()Upgrades the core of WordPress.
- wp_edit_theme_plugin_file()Attempts to edit a file for a theme or plugin.
- wp_opcache_invalidate_directory()Attempts to clear the opcode cache for a directory of files.
Source code
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;}Changelog
Introduced in 5.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.