_build_block_template_result_from_file( array $template_file, string $template_type ): WP_Block_Template
- Since
- 5.9.0, 6.3.0, 7.1.0
- Source
wp-includes/block-template-utils.php:605
Compatibility
- WordPress
- since 7.1.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
- Scaling
- Constant
- Instructions
- 67–131
- Plugin surface
- None
- Called by
- 2
Reads or writes the filesystem via file_get_contents().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 144.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_get_contents()called directly - hookthird-party callbacks
apply_filters()one call below _build_block_template_result_from_file() - optionoption read or write
get_option()one call below _build_block_template_result_from_file() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$template_type !== "wp_template" | 67–93 | get_default_block_template_types(), get_stylesheet(), file_get_contents(), apply_block_hooks_to_content() |
$template_type === "wp_template" | 76–121 | get_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" | 77–103 | get_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" | 86–131 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 144 | 67–131 | 13 | |
| 8.5 | 144 | 67–131 | 13 | |
| 8.4 | 144 | 67–131 | 13 | |
| 8.3 | 144 | 67–131 | 13 | |
| 8.2 | 144 | 67–131 | 13 | |
| 8.1 | 144 | 67–131 | 13 | 6 fewer instructions than PHP 7.4 |
| 7.4 | 150 | 68–134 | 13 |
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
- get_default_block_template_types()Returns a filtered list of default template types, containing their localized titles and descriptions.
- get_stylesheet()Retrieves name of the current stylesheet.
- get_comment_delimited_block_content()Returns the content of a block, including comment delimiters.
- apply_block_hooks_to_content()Runs the hooked blocks algorithm on the given content.
- remove_serialized_parent_block()Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks.
- WP_Block_Template::__construct()
- WP_Block_Templates_Registry::get_instance()::get_by_slug()
- WP_Block_Templates_Registry::get_instance()Utility method to retrieve the main instance of the class.
Used by · 2
- get_block_file_template()Retrieves a unified template object based on a theme file or plugin registration.
- get_block_templates()Retrieves a list of unified template objects based on a query.
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; $template->date = 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.
Signature, return type and hooks compared across 5 parsed releases.
date property to template objects.from the docblockmodified property to template objects.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.