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
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
$loadis 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
- Scaling
- Scales with input
- Instructions
- 19–58
- Plugin surface
- None
- Called by
- 8
Reads or writes the filesystem via file_exists().
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.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_exists()called directly - hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($wp_stylesheet_path) && isset($wp_template_path) | 19–22 | is_child_theme() |
| always | 19–24 | wp_set_template_globals(), is_child_theme() |
isset($wp_stylesheet_path) && isset($wp_template_path) && $located !== "" | 26–27 | is_child_theme(), load_template() |
$located !== "" | 26–29 | wp_set_template_globals(), is_child_theme(), load_template() |
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && file_exists() | 31–33 | is_child_theme(), file_exists() |
!$template_names && file_exists() | 31–35 | wp_set_template_globals(), is_child_theme(), file_exists() |
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names | 38–45 | is_child_theme(), file_exists(), file_exists() |
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && file_exists() && $located !== "" | 38 | is_child_theme(), file_exists(), load_template() |
!$template_names | 38–47 | wp_set_template_globals(), is_child_theme(), file_exists(), file_exists() |
!$template_names && file_exists() && $located !== "" | 38–40 | wp_set_template_globals(), is_child_theme(), file_exists(), load_template() |
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && $located !== "" | 45–50 | is_child_theme(), file_exists(), file_exists(), load_template() |
!$template_names && $located !== "" | 45–52 | wp_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_names | 49–51 | is_child_theme(), file_exists(), file_exists(), file_exists() |
!$template_names | 49–53 | wp_set_template_globals(), is_child_theme(), file_exists(), file_exists(), file_exists() |
isset($wp_stylesheet_path) && isset($wp_template_path) && !$template_names && $located !== "" | 56 | is_child_theme(), file_exists(), file_exists(), file_exists(), load_template() |
!$template_names && $located !== "" | 56–58 | wp_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
- wp_set_template_globals()Set up the globals used for template loading.
- is_child_theme()Whether a child theme is in use.
- load_template()Requires the template file with WordPress environment.
Used by · 8
- get_footer()Loads footer template.
- get_header()Loads header template.
- get_query_template()Retrieves path to a template.
- get_search_form()Displays search form.
- get_sidebar()Loads sidebar template.
- get_template_part()Loads a template part into a template.
- twentyfifteen_author_bio_template()Prevents `author-bio.php` partial template from interfering with rendering an author archive of a user with the `bio` username.
- twentythirteen_author_bio_template()Prevents `author-bio.php` partial template from interfering with rendering an author archive of a user with the `bio` username.
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.
Signature, return type and hooks compared across 5 parsed releases.
$args parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.