WordPress – Hiding or removing parent theme’s templates

I’m producing a child theme for wordpress. I’m trying to make it as fool proof as possible for the end users so would like to remove all the parent’s page templates so that they can’t be accidentally selected.

Is there a way to de-register them from the child themes functions or plugin? I know I could just go and delete them from the parent but that’s not update/upgrade proof.

Read More

Any help appreciated, thanks!

Related posts

Leave a Reply

3 comments

  1. UPDATE 2019-06-10: At the time of writing this answer there was no good way of doing this, but since then the theme_page_templates and theme_templates hooks has been added, which makes this possible. From the theme_page_templates docs page:

    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 );> 
    

    PREVIOUS ANSWER:

    There are no filters available in get_themes() which is where WordPress locates the template files from the theme and parent theme in case one is used. Nor are there any filters available for get_page_templates() or page_template_dropdown(). I would say your best option is to somehow remove or hide the option elements in the admin panel, preventing the user from choosing them.

    Hook onto the admin_head action and inject javascript or css to handle #page_template option[val=template-filename.php].

  2. Simply add empty templates, that have the same name and template header, in your child theme. Then include your own templates from within those template with locate_template();.