wppaste
WordPress

locate_template( string|array $template_names, bool $load = false, bool $load_once = true, array $args = array() ): string

Since
2.7.0, 5.5.0
Source
wp-includes/template.php:722
Retrieves the name of the highest priority template file that exists.

Description

Searches in the stylesheet directory before the template directory and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.

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

$template_namesstring|array
Template file(s) to search for, in order.
$loadbooloptional
If true the template file will be loaded if it is found.Default: false
$load_oncebooloptional
Whether to require_once or require. Has no effect if $load is false.
Default true.Default: true
$argsarrayoptional
Additional arguments passed to the template.
Default empty array.Default: array()

Return value

string
The template filename if one is located.

Performance profile

How much work a call to locate_template() 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 or writes the filesystem via file_exists().

Scaling
Scales with input

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

Instructions
19–58

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
8

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

What it touches

  • filesystemfilesystem accessfile_exists()called directly
  • hookthird-party callbacksdo_action()one call below locate_template()

Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost locate_template() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($wp_stylesheet_path) && isset($wp_template_path)19–22is_child_theme()
always19–24wp_set_template_globals(), is_child_theme()
isset($wp_stylesheet_path) && isset($wp_template_path) && $located !== ""26–27is_child_theme(), load_template()
$located !== ""26–29wp_set_template_globals(), is_child_theme(), load_template()
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && file_exists()31–33is_child_theme(), file_exists()
!$template_names && file_exists()31–35wp_set_template_globals(), is_child_theme(), file_exists()
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names38–45is_child_theme(), file_exists(), file_exists()
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && file_exists() && $located !== ""38is_child_theme(), file_exists(), load_template()
!$template_names38–47wp_set_template_globals(), is_child_theme(), file_exists(), file_exists()
!$template_names && file_exists() && $located !== ""38–40wp_set_template_globals(), is_child_theme(), file_exists(), load_template()
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && $located !== ""45–50is_child_theme(), file_exists(), file_exists(), load_template()
!$template_names && $located !== ""45–52wp_set_template_globals(), is_child_theme(), file_exists(), file_exists(), load_template()
4 further outcomes, up to 58 instructions
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names49–51is_child_theme(), file_exists(), file_exists(), file_exists()
!$template_names49–53wp_set_template_globals(), is_child_theme(), file_exists(), file_exists(), file_exists()
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && $located !== ""56is_child_theme(), file_exists(), file_exists(), file_exists(), load_template()
!$template_names && $located !== ""56–58wp_set_template_globals(), is_child_theme(), file_exists(), file_exists(), file_exists(), load_template()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 66 instructions, 19–58 executed per call, 11 branches. The work does not change between versions.

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 · 3

Used by · 8

Source code

function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {	global $wp_stylesheet_path, $wp_template_path; 	if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {		wp_set_template_globals();	} 	$is_child_theme = is_child_theme(); 	$located = '';	foreach ( (array) $template_names as $template_name ) {		if ( ! $template_name ) {			continue;		}		if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {			$located = $wp_stylesheet_path . '/' . $template_name;			break;		} elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {			$located = $wp_template_path . '/' . $template_name;			break;		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;			break;		}	} 	if ( $load && '' !== $located ) {		load_template( $located, $load_once, $args );	} 	return $located;}

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

5.5.0
The $args parameter was added.from the docblock
2.7.0
Introduced.from the docblock

About this page

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