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
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.
intaxonomy-booksthetaxonomyis 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
- Scaling
- Scales with input
- Instructions
- 10–95
- Plugin surface
- 4 hooks
- Called by
- 1
Touches nothing outside its own arguments.
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 145.
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.
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()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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–13 | apply_filters() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && preg_match() && !$template_type | 45–60 | preg_match(), explode() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && !preg_match() && !$template_type | 47–62 | preg_match(), preg_match(), explode() |
$slug !== "index" && $slug !== "front-page" && !empty($template_prefix) && !$template_type | 48–68 | explode(), explode() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && preg_match() && $template_type | 50–65 | preg_match(), explode(), apply_filters() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && !preg_match() && $template_type | 52–67 | preg_match(), preg_match(), explode(), apply_filters() |
$slug !== "index" && $slug !== "front-page" && !empty($template_prefix) && $template_type | 53–73 | explode(), explode(), apply_filters() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type !== "single" && !$template_type | 57–89 | preg_match(), preg_match(), get_taxonomies(), explode() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type === "single" && !$template_type | 58–90 | preg_match(), preg_match(), get_post_types(), explode() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type !== "single" && $template_type | 62–94 | preg_match(), preg_match(), get_taxonomies(), explode(), apply_filters() |
$slug !== "index" && $slug !== "front-page" && empty($template_prefix) && $type === "single" && $template_type | 63–95 | preg_match(), preg_match(), get_post_types(), explode(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 143 | 10–94 | 25 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 145 | 10–95 | 25 | |
| 8.4 | 145 | 10–95 | 25 | 24 fewer instructions than PHP 8.3 |
| 8.3 | 169 | 10–116 | 25 | |
| 8.2 | 169 | 10–116 | 25 | |
| 8.1 | 169 | 10–116 | 25 | |
| 7.4 | 169 | 10–116 | 25 |
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:
- apply_filters( index_template_hierarchy )filterline 1595 (+3 into the body)
- apply_filters( page_template_hierarchy )filterline 1599 (+7 into the body)
- apply_filters( frontpage_template_hierarchy )filterline 1603 (+11 into the body)
- apply_filters( {$template_type}_template_hierarchy )filterline 1679 (+87 into the body)
Uses · 4
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_post_types()Gets a list of all registered post type objects.
- get_taxonomies()Retrieves a list of registered taxonomy names or objects.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Used by · 1
- WP_REST_Templates_Controller::get_template_fallback()Returns the fallback template for the given slug.
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.
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.