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

Filters list of page templates for a theme.


Description Description


Parameters Parameters

$page_templates

(array) Array of page templates. Keys are filenames, values are translated names.

$this

(WP_Theme) The theme object.

$post

(WP_Post|null) The post being edited, provided for context, or null.


Top ↑

Source Source

File: wp-includes/class-wp-theme.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Converted to allow complete control over the $page_templates array.
3.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by SpartakusMd

    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.