_get_block_templates_files( string $template_type, array $query = array() ): array|null
- Since
- 5.9.0, 6.3.0
- Source
wp-includes/block-template-utils.php:374
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_typestring- Template type. Either 'wp_template' or 'wp_template_part'.
$queryarrayoptional- Arguments to retrieve templates. Optional, empty by default.Default:
array()$slug__instring[]List of slugs to include.$slug__not_instring[]List of slugs to skip.$areastringA 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).$post_typestringPost type to get the templates for.
Return value
array|null- Template files on success, null if
$template_typeis not matched.
Performance profile
How much work a call to _get_block_templates_files() 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
- 41–55
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 142.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below _get_block_templates_files() - optionoption read or write
get_option()one call below _get_block_templates_files()
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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_block_templates_files() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$template_type !== "wp_template" && $stylesheet === false | 41–48 | get_stylesheet(), get_template(), get_stylesheet_directory(), array_values() |
$template_type === "wp_template" && $stylesheet === false | 44–51 | get_default_block_template_types(), get_stylesheet(), get_template(), get_stylesheet_directory(), array_values() |
$template_type !== "wp_template" && $stylesheet !== false | 45–52 | get_stylesheet(), get_template(), get_stylesheet_directory(), get_template_directory(), array_values() |
$template_type === "wp_template" && $stylesheet !== false | 48–55 | get_default_block_template_types(), get_stylesheet(), get_template(), get_stylesheet_directory(), get_template_directory(), array_values() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 142 | 41–55 | 28 | |
| 8.5 | 142 | 41–55 | 28 | |
| 8.4 | 142 | 41–55 | 28 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 157 | 41–54 | 28 | |
| 8.2 | 157 | 41–54 | 28 | |
| 8.1 | 157 | 41–54 | 28 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 161 | 41–57 | 28 |
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 · 9
- 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_template()Retrieves name of the active theme.
- get_stylesheet_directory()Retrieves stylesheet directory path for 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.
- _get_block_templates_paths()Finds all nested template part file paths in a theme's directory.
- _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 · 1
- get_block_templates()Retrieves a list of unified template objects based on a query.
Source code
function _get_block_templates_files( $template_type, $query = array() ) { if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } $default_template_types = array(); if ( 'wp_template' === $template_type ) { $default_template_types = get_default_block_template_types(); } // Prepare metadata from $query. $slugs_to_include = isset( $query['slug__in'] ) ? $query['slug__in'] : array(); $slugs_to_skip = isset( $query['slug__not_in'] ) ? $query['slug__not_in'] : array(); $area = isset( $query['area'] ) ? $query['area'] : null; $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $stylesheet = get_stylesheet(); $template = get_template(); $themes = array( $stylesheet => get_stylesheet_directory(), ); // Add the parent theme if it's not the same as the current theme. if ( $stylesheet !== $template ) { $themes[ $template ] = get_template_directory(); } $template_files = array(); foreach ( $themes as $theme_slug => $theme_dir ) { $template_base_paths = get_block_theme_folders( $theme_slug ); $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { $template_base_path = $template_base_paths[ $template_type ]; $template_slug = substr( $template_file, // Starting position of slug. strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ), // Subtract ending '.html'. -5 ); // Skip this item if its slug doesn't match any of the slugs to include. if ( ! empty( $slugs_to_include ) && ! in_array( $template_slug, $slugs_to_include, true ) ) { continue; } // Skip this item if its slug matches any of the slugs to skip. if ( ! empty( $slugs_to_skip ) && in_array( $template_slug, $slugs_to_skip, true ) ) { continue; } /* * The child theme items (stylesheet) are processed before the parent theme's (template). * If a child theme defines a template, prevent the parent template from being added to the list as well. */ if ( isset( $template_files[ $template_slug ] ) ) { continue; } $new_template_item = array( 'slug' => $template_slug, 'path' => $template_file, 'theme' => $theme_slug, 'type' => $template_type, ); if ( 'wp_template_part' === $template_type ) { $candidate = _add_block_template_part_area_info( $new_template_item ); if ( ! isset( $area ) || $area === $candidate['area'] ) { $template_files[ $template_slug ] = $candidate; } } if ( 'wp_template' === $template_type ) { $candidate = _add_block_template_info( $new_template_item ); $is_custom = ! isset( $default_template_types[ $candidate['slug'] ] ); if ( ! $post_type || ( $post_type && isset( $candidate['postTypes'] ) && in_array( $post_type, $candidate['postTypes'], true ) ) ) { $template_files[ $template_slug ] = $candidate;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.
$query parameter.from the docblockAbout 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.