_wp_is_template_path_allowed( string $path ): bool
- Since
- 7.1.2
- Source
wp-includes/template.php:713
Compatibility
- WordPress
- since 7.1.2
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pathstring- Path to an existing template file.
Return value
bool- Whether the template may be loaded.
Performance profile
How much work a call to _wp_is_template_path_allowed() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 10–62
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 66.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below _wp_is_template_path_allowed() - optionoption read or write
get_option()one call below _wp_is_template_path_allowed()
Further down the call graph this can also reach cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
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_is_template_path_allowed() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | wp_normalize_path() |
$real_path === false | 16 | wp_normalize_path(), realpath() |
$real_path !== false | 41–48 | wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit(), get_stylesheet(), get_template() |
$real_path !== false && !$directories && $real_directory !== false && $real_path | 56–62 | wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit(), get_stylesheet(), get_template(), realpath(), wp_normalize_path(), trailingslashit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 66 | 10–62 | 8 | |
| 8.5 | 66 | 10–62 | 8 | |
| 8.4 | 66 | 10–62 | 8 | 16 fewer instructions than PHP 8.3 |
| 8.3 | 82 | 13–78 | 8 | |
| 8.2 | 82 | 13–78 | 8 | |
| 8.1 | 82 | 13–78 | 8 | |
| 7.4 | 82 | 13–78 | 8 |
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.
Uses · 6
- wp_normalize_path()Normalizes a filesystem path.
- trailingslashit()Appends a trailing slash.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_stylesheet()Retrieves name of the current stylesheet.
- get_template()Retrieves name of the active theme.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Used by · 1
- locate_template()Retrieves the name of the highest priority template file that exists.
Source code
function _wp_is_template_path_allowed( $path ) { global $wp_stylesheet_path, $wp_template_path; // A file path that exists and does not contain `..` is allowed. if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) { return true; } // Resolve the true location of the requested file for later comparison. $real_path = realpath( $path ); if ( false === $real_path ) { return false; } $real_path = trailingslashit( wp_normalize_path( $real_path ) ); $directories = array( $wp_stylesheet_path, $wp_template_path, ABSPATH . WPINC . '/theme-compat', ); // If a theme is in a subdirectory, accept templates from its direct parent directory. if ( str_contains( get_stylesheet(), '/' ) ) { $directories[] = dirname( $wp_stylesheet_path ); } // If a parent theme is in a subdirectory, accept templates from its direct parent directory. if ( str_contains( get_template(), '/' ) ) { $directories[] = dirname( $wp_template_path ); } foreach ( $directories as $directory ) { $real_directory = realpath( $directory ); if ( false === $real_directory ) { continue; } // The true location of the requested file must be inside one of the allowed directories. if ( str_starts_with( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) { return true; } } return false;}Changelog
Introduced in 7.1.2. Unchanged from 6.7.10 through 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.3 tag, from
src/wp-includes/template.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.