Filter hook 'page_template_hierarchy'

in WP Core File wp-includes/block-template-utils.php at line 1571

View Source

page_template_hierarchy

Filter Hook
Description
Gets the template hierarchy for the given template slug to be created. Note: Always add `index` as the last fallback template.

Hook Information

File Location wp-includes/block-template-utils.php View on GitHub
Hook Type Filter
Line Number 1571

Hook Parameters

Type Name Description
string $slug The template slug to be created.
bool $is_custom Optional. Indicates if a template is custom or part of the template hierarchy. Default false.
string $template_prefix Optional. 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.

Usage Examples

Basic Usage
<?php
// Hook into page_template_hierarchy
add_filter('page_template_hierarchy', 'my_custom_filter', 10, 3);

function my_custom_filter($slug, $is_custom, $template_prefix) {
    // Your custom filtering logic here
    return $slug;
}

Source Code Context

wp-includes/block-template-utils.php:1571 - How this hook is used in WordPress core
<?php
1566  		/** This filter is documented in wp-includes/template.php */
1567  		return apply_filters( 'index_template_hierarchy', array( 'index' ) );
1568  	}
1569  	if ( $is_custom ) {
1570  		/** This filter is documented in wp-includes/template.php */
1571  		return apply_filters( 'page_template_hierarchy', array( 'page', 'singular', 'index' ) );
1572  	}
1573  	if ( 'front-page' === $slug ) {
1574  		/** This filter is documented in wp-includes/template.php */
1575  		return apply_filters( 'frontpage_template_hierarchy', array( 'front-page', 'home', 'index' ) );
1576  	}

PHP Documentation

<?php
/**
 * Gets the template hierarchy for the given template slug to be created.
 *
 * Note: Always add `index` as the last fallback template.
 *
 * @since 6.1.0
 *
 * @param string $slug            The template slug to be created.
 * @param bool   $is_custom       Optional. Indicates if a template is custom or
 *                                part of the template hierarchy. Default false.
 * @param string $template_prefix Optional. 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.
 * @return string[] The template hierarchy.
 */
Quick Info
  • Hook Type: Filter
  • Parameters: 3
  • File: wp-includes/block-template-utils.php
Related Hooks

Related hooks will be displayed here in future updates.