wppaste
WordPress

_wp_is_template_path_allowed( string $path ): bool

Since
7.1.2
Source
wp-includes/template.php:713
Determines whether a template found by locate_template() may be loaded.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
10–62

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 66.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below _wp_is_template_path_allowed()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
always10wp_normalize_path()
$real_path === false16wp_normalize_path(), realpath()
$real_path !== false41–48wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit(), get_stylesheet(), get_template()
$real_path !== false && !$directories && $real_directory !== false && $real_path56–62wp_normalize_path(), realpath(), wp_normalize_path(), trailingslashit(), get_stylesheet(), get_template(), realpath(), wp_normalize_path(), trailingslashit()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6610–628
8.56610–628
8.46610–62816 fewer instructions than PHP 8.3
8.38213–788
8.28213–788
8.18213–788
7.48213–788

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

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.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.10 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.