apply_filters( ‘theme_page_templates’, array $page_templates, WP_Theme $this, WP_Post|null $post )

Filters list of page templates for a theme.

Parameters

$page_templatesarray
Array of page templates. Keys are filenames, values are translated names.
$thisWP_Theme
The theme object.
$postWP_Post|null
The post being edited, provided for context, or null.

Source


Changelog

VersionDescription
4.4.0Converted to allow complete control over the $page_templates array.
3.9.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    If you want to remove templates from both Pages and Posts, you will need to also use the `theme_post_templates` in addition to the `theme_page_templates` filter.

    /* Remove parent theme page templates */
    
    function child_remove_page_templates( $page_templates ) {
      unset( $page_templates['template-cover.php'] );
      unset( $page_templates['template-full-width-cover.php'] );
      unset( $page_templates['template-full-width-only-content.php'] );
      unset( $page_templates['template-full-width.php'] );
      unset( $page_templates['template-only-content.php'] );
    
      return $page_templates;
    }
    add_filter( 'theme_page_templates', 'child_remove_page_templates' );
    add_filter( 'theme_post_templates', 'child_remove_page_templates' );
  2. Skip to note 5 content

    Filter page templates by blog id

    Suppose you have the blog `Food` with the id 2 and the template `page-food.php` which should only be used for this blog. The example below removes the page template from dropdowns of other blogs:

    /**
     * Filter the theme page templates.
     *
     * @param array    $page_templates Page templates.
     * @param WP_Theme $this           WP_Theme instance.
     * @param WP_Post  $post           The post being edited, provided for context, or null.
     * @return array (Maybe) modified page templates array.
     */
    function wpdocs_filter_theme_page_templates( $page_templates, $this, $post ) {
    	$current_blog_id = get_current_blog_id();
    	$food_blog_id    = 2;
    
    	if ( $current_blog_id != $food_blog_id ) {
    		if ( isset( $page_templates['page-food.php'] ) ) {
    			unset( $page_templates['page-food.php'] );
    		}
    	}
    	return $page_templates;
    }
    add_filter( 'theme_page_templates', 'wpdocs_filter_theme_page_templates', 20, 3 );

You must log in before being able to contribute a note or feedback.