wppaste
WordPress

_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
Retrieves the template files from the theme.

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.
  • $areastring

    A 'wp_template_part_area' taxonomy value to filter by (for 'wp_template_part' template type only).
  • $post_typestring

    Post type to get the templates for.

Return value

array|null
Template files on success, null if $template_type is 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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
41–55

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
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below _get_block_templates_files()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
$template_type !== "wp_template" && $stylesheet === false41–48get_stylesheet(), get_template(), get_stylesheet_directory(), array_values()
$template_type === "wp_template" && $stylesheet === false44–51get_default_block_template_types(), get_stylesheet(), get_template(), get_stylesheet_directory(), array_values()
$template_type !== "wp_template" && $stylesheet !== false45–52get_stylesheet(), get_template(), get_stylesheet_directory(), get_template_directory(), array_values()
$template_type === "wp_template" && $stylesheet !== false48–55get_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

PHPCompiledExecutedBranchesNotes
8.6-dev14241–5528
8.514241–5528
8.414241–552815 fewer instructions than PHP 8.3
8.315741–5428
8.215741–5428
8.115741–54284 fewer instructions than PHP 7.4
7.416141–5728

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

Used by · 1

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.

  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 the $query parameter.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.