_get_block_template_file( string $template_type, string $slug ): array|null
- Since
- 5.9.0
- Source
wp-includes/block-template-utils.php:323
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
$template_typestring- Template type. Either 'wp_template' or 'wp_template_part'.
$slugstring- Template slug.
Return value
array|null- Array with template metadata if $template_type is one of 'wp_template' or 'wp_template_part', null otherwise.
$slugstringTemplate slug.$pathstringTemplate file path.$themestringTheme slug.$typestringTemplate type.$areastringTemplate area. Only for 'wp_template_part'.$titlestringOptional. Template title.$postTypesstring[]Optional. List of post types that the template supports. Only for 'wp_template'.
Performance profile
How much work a call to _get_block_template_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
- Scales with input
- Instructions
- 7–49
- Plugin surface
- None
- Called by
- 5
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 59.
Nothing here hands control to plugin code.
5 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
apply_filters()one call below _get_block_template_file() - optionoption read or write
get_option()one call below _get_block_template_file()
Further down the call graph this can also reach 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 · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_block_template_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$template_type !== "wp_template" && $template_type !== "wp_template_part" | 7 | none |
| always | 18–21 | get_stylesheet(), get_stylesheet_directory(), get_template(), get_template_directory() |
!$themes && file_exists() && $template_type !== "wp_template_part" && $template_type !== "wp_template" | 44–46 | get_stylesheet(), get_stylesheet_directory(), get_template(), get_template_directory(), get_block_theme_folders(), file_exists() |
!$themes && file_exists() && $template_type === "wp_template_part" | 45–47 | get_stylesheet(), get_stylesheet_directory(), get_template(), get_template_directory(), get_block_theme_folders(), file_exists(), _add_block_template_part_area_info() |
$template_type === "wp_template" && !$themes && file_exists() && $template_type !== "wp_template_part" | 47–49 | get_stylesheet(), get_stylesheet_directory(), get_template(), get_template_directory(), get_block_theme_folders(), file_exists(), _add_block_template_info() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 59 instructions, 7–49 executed per call, 7 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 · 7
- get_stylesheet()Retrieves name of the current stylesheet.
- get_stylesheet_directory()Retrieves stylesheet directory path for the active theme.
- get_template()Retrieves name of the active theme.
- get_template_directory()Retrieves template directory path for the active theme.
- get_block_theme_folders()For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly.
- _add_block_template_part_area_info()Attempts to add the template part's area information to the input template.
- _add_block_template_info()Attempts to add custom template information to the template item.
Used by · 5
- _build_block_template_object_from_post_object()Builds a block template object from a post object.
- get_block_file_template()Retrieves a unified template object based on a theme file.
- locate_block_template()Finds a block template with equal or higher specificity than a given PHP template file.
- render_block_core_template_part()Renders the `core/template-part` block on the server.
- resolve_block_template()Returns the correct 'wp_template' to render for the request template type.
Source code
function _get_block_template_file( $template_type, $slug ) { if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } $themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); foreach ( $themes as $theme_slug => $theme_dir ) { $template_base_paths = get_block_theme_folders( $theme_slug ); $file_path = $theme_dir . '/' . $template_base_paths[ $template_type ] . '/' . $slug . '.html'; if ( file_exists( $file_path ) ) { $new_template_item = array( 'slug' => $slug, 'path' => $file_path, 'theme' => $theme_slug, 'type' => $template_type, ); if ( 'wp_template_part' === $template_type ) { return _add_block_template_part_area_info( $new_template_item ); } if ( 'wp_template' === $template_type ) { return _add_block_template_info( $new_template_item ); } return $new_template_item; } } return null;}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.8.8 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.