wppaste
WordPress

_build_block_template_result_from_file( array $template_file, string $template_type ): WP_Block_Template

Since
5.9.0, 6.3.0
Source
wp-includes/block-template-utils.php:592
Builds a unified template object based on a theme file.

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

$template_filearray
Theme file.
$template_typestring
Template type. Either 'wp_template' or 'wp_template_part'.

Return value

WP_Block_Template
Template.

Performance profile

How much work a call to _build_block_template_result_from_file() 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
65–129

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places 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 _build_block_template_result_from_file()
  • optionoption read or writeget_option()one call below _build_block_template_result_from_file()
  • querycontent queryget_post()one call below _build_block_template_result_from_file()

Further down the call graph this can also reach 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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
$template_type !== "wp_template"65–91get_default_block_template_types(), get_stylesheet(), file_get_contents(), apply_block_hooks_to_content()
$template_type === "wp_template"74–119get_default_block_template_types(), get_stylesheet(), file_get_contents(), ::WP_Block_Templates_Registry(), ->get_by_slug(), apply_block_hooks_to_content()
$template_type !== "wp_template"75–101get_default_block_template_types(), get_stylesheet(), file_get_contents(), get_comment_delimited_block_content(), apply_block_hooks_to_content(), remove_serialized_parent_block()
$template_type === "wp_template"84–129get_default_block_template_types(), get_stylesheet(), file_get_contents(), ::WP_Block_Templates_Registry(), ->get_by_slug(), get_comment_delimited_block_content(), apply_block_hooks_to_content(), remove_serialized_parent_block()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14265–12913
8.514265–12913
8.414265–12913
8.314265–12913
8.214265–12913
8.114265–129136 fewer instructions than PHP 7.4
7.414866–13213

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

Used by · 2

Source code

function _build_block_template_result_from_file( $template_file, $template_type ) {	$default_template_types = get_default_block_template_types();	$theme                  = get_stylesheet(); 	$template                 = new WP_Block_Template();	$template->id             = $theme . '//' . $template_file['slug'];	$template->theme          = $theme;	$template->content        = file_get_contents( $template_file['path'] );	$template->slug           = $template_file['slug'];	$template->source         = 'theme';	$template->type           = $template_type;	$template->title          = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];	$template->status         = 'publish';	$template->has_theme_file = true;	$template->is_custom      = true;	$template->modified       = null; 	if ( 'wp_template' === $template_type ) {		$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );		if ( $registered_template ) {			$template->plugin      = $registered_template->plugin;			$template->title       = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;			$template->description = empty( $template->description ) ? $registered_template->description : $template->description;		}	} 	if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {		$template->description = $default_template_types[ $template_file['slug'] ]['description'];		$template->title       = $default_template_types[ $template_file['slug'] ]['title'];		$template->is_custom   = false;	} 	if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {		$template->post_types = $template_file['postTypes'];	} 	if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) {		$template->area = $template_file['area'];	} 	if ( 'wp_template_part' === $template->type ) {		/*		 * In order for hooked blocks to be inserted at positions first_child and last_child in a template part,		 * we need to wrap its content a mock template part block and traverse it.		 */		$content           = get_comment_delimited_block_content(			'core/template-part',			array(),			$template->content		);		$content           = apply_block_hooks_to_content(			$content,			$template,			'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'		);		$template->content = remove_serialized_parent_block( $content );	} else {		$template->content = apply_block_hooks_to_content(			$template->content,			$template,			'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'		);	} 	return $template;}

Changelog

Introduced in 5.9.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 modified property to template objects.from the docblock
5.9.0
Introduced.from the docblock

About this page

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