get_block_file_template( string $id, string $template_type = 'wp_template' ): WP_Block_Template|null
- Since
- 5.9.0
- Source
wp-includes/block-template-utils.php:1351
Description
This is a fallback of get_block_template(), used when no templates are found in the database.
Also checks for templates registered via the Template Registration API.
Compatibility
- WordPress
- since 5.9.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
$idstring- Template unique identifier (example: 'theme_slug//template_slug').
$template_typestringoptional- Template type. Either 'wp_template' or 'wp_template_part'.
Default 'wp_template'.Default:'wp_template'
Return value
WP_Block_Template|null- The found block template, or null if there isn't one.
Performance profile
How much work a call to get_block_file_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
- Constant
- Instructions
- 12–58
- Plugin surface
- 2 hooks
- Called by
- 3
Reaches the database via get_post().
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 78.
Third-party callbacks on 'pre_get_block_file_template', 'get_block_file_template' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_block_file_template() - querycontent query
get_post()one call below get_block_file_template()
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 · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_block_file_template() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$block_template !== null | 12 | apply_filters() |
$block_template === null | 27 | apply_filters(), explode(), apply_filters() |
$block_template === null && $theme !== false | 42 | apply_filters(), explode(), get_stylesheet(), ::WP_Block_Templates_Registry(), ->get_by_slug(), apply_filters() |
$block_template === null && $theme === false && $template_file !== null | 47 | apply_filters(), explode(), get_stylesheet(), _get_block_template_file(), _build_block_template_result_from_file(), apply_filters() |
$block_template === null && $theme === false && $template_file === null | 49 | apply_filters(), explode(), get_stylesheet(), _get_block_template_file(), ::WP_Block_Templates_Registry(), ->get_by_slug(), apply_filters() |
$block_template === null && $theme !== false | 51 | apply_filters(), explode(), get_stylesheet(), ::WP_Block_Templates_Registry(), ->get_by_slug(), apply_block_hooks_to_content(), apply_filters() |
$block_template === null && $theme === false && $template_file === null | 58 | apply_filters(), explode(), get_stylesheet(), _get_block_template_file(), ::WP_Block_Templates_Registry(), ->get_by_slug(), apply_block_hooks_to_content(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 78 instructions, 12–58 executed per call, 5 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.
Hooks and filters fired · 4
4 hooks fire while get_block_file_template() runs, in this order:
- apply_filters( pre_get_block_file_template )filterline 1364 (+13 into the body)
Filters the block template object before the theme file discovery takes place.
- apply_filters( get_block_file_template )filterline 1372 (+21 into the body)
Filters the block template object after it has been (potentially) fetched from the theme file.
- apply_filters( get_block_file_template )filterline 1382 (+31 into the body)
Filters the block template object after it has been (potentially) fetched from the theme file.
- apply_filters( get_block_file_template )filterline 1405 (+54 into the body)
Filters the block template object after it has been (potentially) fetched from the theme file.
Uses · 7
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_stylesheet()Retrieves name of the current stylesheet.
- _get_block_template_file()Retrieves the template file from the theme for a given slug.
- _build_block_template_result_from_file()Builds a unified template object based on a theme file.
- apply_block_hooks_to_content()Runs the hooked blocks algorithm on the given content.
- 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 · 3
- WP_REST_Templates_Controller::get_item()Returns the given template
- get_block_template()Retrieves a single unified template object using its id.
- render_block_core_template_part()Renders the `core/template-part` block on the server.
Source code
function get_block_file_template( $id, $template_type = 'wp_template' ) { /** * Filters the block template object before the theme file discovery takes place. * * Return a non-null value to bypass the WordPress theme file discovery. * * @since 5.9.0 * * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, * or null to allow WP to run its normal queries. * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. */ $block_template = apply_filters( 'pre_get_block_file_template', null, $id, $template_type ); if ( ! is_null( $block_template ) ) { return $block_template; } $parts = explode( '//', $id, 2 ); if ( count( $parts ) < 2 ) { /** This filter is documented in wp-includes/block-template-utils.php */ return apply_filters( 'get_block_file_template', null, $id, $template_type ); } list( $theme, $slug ) = $parts; if ( get_stylesheet() === $theme ) { $template_file = _get_block_template_file( $template_type, $slug ); if ( null !== $template_file ) { $block_template = _build_block_template_result_from_file( $template_file, $template_type ); /** This filter is documented in wp-includes/block-template-utils.php */ return apply_filters( 'get_block_file_template', $block_template, $id, $template_type ); } } $block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug ); if ( $block_template ) { $block_template->content = apply_block_hooks_to_content( $block_template->content, $block_template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' ); } /** * Filters the block template object after it has been (potentially) fetched from the theme file. * * @since 5.9.0 * * @param WP_Block_Template|null $block_template The found block template, or null if there is none. * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. */ return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );}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.
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.