wppaste
WordPress

locate_block_template( string $template, string $type, string[] $templates ): string

Since
5.8.0, 6.3.0
Source
wp-includes/block-template.php:62
Finds a block template with equal or higher specificity than a given PHP template file.

Description

Internally, this communicates the block content that needs to be used by the template canvas through a global variable.

Compatibility

WordPress
since 6.3.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

$templatestring
Path to the template. See locate_template().
$typestring
Sanitized filename without extension.
$templatesstring[]
A list of template candidates, in descending order of priority.

Return value

string
The path to the Site Editor template canvas file, or the fallback PHP template.

Performance profile

How much work a call to locate_block_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_get_contents().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
10–89

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

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

  • filesystemfilesystem accessfile_get_contents()called directly
  • hookthird-party callbacksapply_filters()one call below locate_block_template()

Further down the call graph this can also reach option, query, cache, serialize 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 · 21 distinct outcomes

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

WhenInstructionsCalls it makes
!current_theme_supports()10current_theme_supports()
current_theme_supports()19–21current_theme_supports(), resolve_block_template()
current_theme_supports()42–44current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template()
current_theme_supports() && !isset($value)43–48current_theme_supports(), resolve_block_template(), add_action(), remove_action(), add_action()
current_theme_supports() && !empty($block_template) && isset($value)50–52current_theme_supports(), resolve_block_template(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && $type === "index" && isset($value)51current_theme_supports(), resolve_block_template(), __(), message(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && !$block_template && !isset($value)52current_theme_supports(), resolve_block_template(), is_user_logged_in(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && is_user_logged_in() && !isset($value)52current_theme_supports(), resolve_block_template(), is_user_logged_in(), wp_render_empty_block_template_warning(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && !$block_template && isset($value)56current_theme_supports(), resolve_block_template(), is_user_logged_in(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && is_user_logged_in() && isset($value)56current_theme_supports(), resolve_block_template(), is_user_logged_in(), wp_render_empty_block_template_warning(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && $block_template && !isset($value)62current_theme_supports(), resolve_block_template(), is_user_logged_in(), _get_block_template_file(), file_get_contents(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && $block_template && isset($value)66current_theme_supports(), resolve_block_template(), is_user_logged_in(), _get_block_template_file(), file_get_contents(), wp_send_json_success(), add_action(), remove_action(), add_action()
9 further outcomes, up to 89 instructions
current_theme_supports() && !isset($value)66–71current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), add_action(), remove_action(), add_action()
current_theme_supports() && !empty($block_template) && isset($value)73–75current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && $type === "index" && isset($value)74current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), __(), message(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && !$block_template && !isset($value)75current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && is_user_logged_in() && !isset($value)75current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), wp_render_empty_block_template_warning(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && !$block_template && isset($value)79current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && is_user_logged_in() && isset($value)79current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), wp_render_empty_block_template_warning(), wp_send_json_success(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && $block_template && !isset($value)85current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), _get_block_template_file(), file_get_contents(), add_action(), remove_action(), add_action()
current_theme_supports() && empty($block_template) && !is_user_logged_in() && $block_template && isset($value)89current_theme_supports(), get_stylesheet_directory(), get_template_directory(), array_search(), array_slice(), resolve_block_template(), is_user_logged_in(), _get_block_template_file(), file_get_contents(), wp_send_json_success(), add_action(), remove_action(), add_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11810–8911
8.511810–8911
8.411810–89114 fewer instructions than PHP 8.3
8.312210–9311
8.212210–9311
8.112210–9311
7.412210–9311

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

Used by · 1

Source code

function locate_block_template( $template, $type, array $templates ) {	global $_wp_current_template_content, $_wp_current_template_id; 	if ( ! current_theme_supports( 'block-templates' ) ) {		return $template;	} 	if ( $template ) {		/*		 * locate_template() has found a PHP template at the path specified by $template.		 * That means that we have a fallback candidate if we cannot find a block template		 * with higher specificity.		 *		 * Thus, before looking for matching block themes, we shorten our list of candidate		 * templates accordingly.		 */ 		// Locate the index of $template (without the theme directory path) in $templates.		$relative_template_path = str_replace(			array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),			'',			$template		);		$index                  = array_search( $relative_template_path, $templates, true ); 		// If the template hierarchy algorithm has successfully located a PHP template file,		// we will only consider block templates with higher or equal specificity.		$templates = array_slice( $templates, 0, $index + 1 );	} 	$block_template = resolve_block_template( $type, $templates, $template ); 	if ( $block_template ) {		$_wp_current_template_id = $block_template->id; 		if ( empty( $block_template->content ) ) {			if ( is_user_logged_in() ) {				$_wp_current_template_content = wp_render_empty_block_template_warning( $block_template );			} else {				if ( $block_template->has_theme_file ) {					// Show contents from theme template if user is not logged in.					$theme_template               = _get_block_template_file( 'wp_template', $block_template->slug );					$_wp_current_template_content = file_get_contents( $theme_template['path'] );				} else {					$_wp_current_template_content = $block_template->content;				}			}		} elseif ( ! empty( $block_template->content ) ) {			$_wp_current_template_content = $block_template->content;		}		if ( isset( $_GET['_wp-find-template'] ) ) {			wp_send_json_success( $block_template );		}	} else {		if ( $template ) {			return $template;		} 		if ( 'index' === $type ) {			if ( isset( $_GET['_wp-find-template'] ) ) {				wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );			}		} else {			return ''; // So that the template loader keeps looking for templates.		}	} 	// Add hooks for template canvas.	// Add viewport meta tag.	add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 ); 	// Render title tag with content, regardless of whether theme has title-tag support.	remove_action( 'wp_head', '_wp_render_title_tag', 1 );    // Remove conditional title tag rendering...	add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional. 	// This file will be included instead of the theme's template file.	return ABSPATH . WPINC . '/template-canvas.php';}

Changelog

Introduced in 5.8.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.

6.3.0
Added $_wp_current_template_id global for editing of current template directly from the admin bar.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/block-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.