wppaste
WordPress

get_template_hierarchy( string $slug, bool $is_custom = false, string $template_prefix = '' ): string[]

Since
6.1.0
Source
wp-includes/block-template-utils.php:1592
Gets the template hierarchy for the given template slug to be created.

Description

Note: Always add index as the last fallback template.

Compatibility

WordPress
since 6.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

$slugstring
The template slug to be created.
$is_custombooloptional
Indicates if a template is custom or part of the template hierarchy. Default false.Default: false
$template_prefixstringoptional
The template prefix for the created template.
Used to extract the main template type, e.g.
in taxonomy-books the taxonomy is extracted.
Default empty string.Default: ''

Return value

string[]
The template hierarchy.

Performance profile

How much work a call to get_template_hierarchy() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
10–95

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 145.

Plugin surface
4 hooks

Third-party callbacks on 'index_template_hierarchy', 'page_template_hierarchy', 'frontpage_template_hierarchy' run inside this call, and their cost is not bounded by anything here.

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()called directly

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_template_hierarchy() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10–13apply_filters()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && preg_match() && !$template_type45–60preg_match(), explode()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && !preg_match() && !$template_type47–62preg_match(), preg_match(), explode()
$slug !== "index" && $slug !== "front-page" && !empty($template_prefix) && !$template_type48–68explode(), explode()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && preg_match() && $template_type50–65preg_match(), explode(), apply_filters()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && !preg_match() && $template_type52–67preg_match(), preg_match(), explode(), apply_filters()
$slug !== "index" && $slug !== "front-page" && !empty($template_prefix) && $template_type53–73explode(), explode(), apply_filters()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type !== "single" && !$template_type57–89preg_match(), preg_match(), get_taxonomies(), explode()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type === "single" && !$template_type58–90preg_match(), preg_match(), get_post_types(), explode()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type !== "single" && $template_type62–94preg_match(), preg_match(), get_taxonomies(), explode(), apply_filters()
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type === "single" && $template_type63–95preg_match(), preg_match(), get_post_types(), explode(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14310–94252 fewer instructions than PHP 8.5
8.514510–9525
8.414510–952524 fewer instructions than PHP 8.3
8.316910–11625
8.216910–11625
8.116910–11625
7.416910–11625

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_template_hierarchy() runs, in this order:

  1. apply_filters( index_template_hierarchy )filterline 1595 (+3 into the body)
  2. apply_filters( page_template_hierarchy )filterline 1599 (+7 into the body)
  3. apply_filters( frontpage_template_hierarchy )filterline 1603 (+11 into the body)
  4. apply_filters( {$template_type}_template_hierarchy )filterline 1679 (+87 into the body)

Uses · 4

Used by · 1

Source code

function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {	if ( 'index' === $slug ) {		/** This filter is documented in wp-includes/template.php */		return apply_filters( 'index_template_hierarchy', array( 'index' ) );	}	if ( $is_custom ) {		/** This filter is documented in wp-includes/template.php */		return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) );	}	if ( 'front-page' === $slug ) {		/** This filter is documented in wp-includes/template.php */		return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) );	} 	$matches = array(); 	$template_hierarchy = array( $slug );	// Most default templates don't have `$template_prefix` assigned.	if ( ! empty( $template_prefix ) ) {		list( $type ) = explode( '-', $template_prefix );		// We need these checks because we always add the `$slug` above.		if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {			$template_hierarchy[] = $template_prefix;		}		if ( $slug !== $type ) {			$template_hierarchy[] = $type;		}	} elseif ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) {		$template_hierarchy[] = $matches[1];	} elseif ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) {		$type           = $matches[1];		$slug_remaining = $matches[2]; 		$items = 'single' === $type ? get_post_types() : get_taxonomies();		foreach ( $items as $item ) {			if ( ! str_starts_with( $slug_remaining, $item ) ) {					continue;			} 			// If $slug_remaining is equal to $post_type or $taxonomy we have			// the single-$post_type template or the taxonomy-$taxonomy template.			if ( $slug_remaining === $item ) {				$template_hierarchy[] = $type;				break;			} 			// If $slug_remaining is single-$post_type-$slug template.			if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) {				$template_hierarchy[] = "$type-$item";				$template_hierarchy[] = $type;				break;			}		}	}	// Handle `archive` template.	if (		str_starts_with( $slug, 'author' ) ||		str_starts_with( $slug, 'taxonomy' ) ||		str_starts_with( $slug, 'category' ) ||		str_starts_with( $slug, 'tag' ) ||		'date' === $slug	) {		$template_hierarchy[] = 'archive';	}	// Handle `single` template.	if ( 'attachment' === $slug ) {		$template_hierarchy[] = 'single';	}	// Handle `singular` template.	if (		str_starts_with( $slug, 'single' ) ||		str_starts_with( $slug, 'page' ) ||		'attachment' === $slug	) {		$template_hierarchy[] = 'singular';	}	$template_hierarchy[] = 'index'; 	$template_type = '';	if ( ! empty( $template_prefix ) ) {

Changelog

Introduced in 6.1.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.

About 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.