How to *remove* a parent theme page template from a child theme?

I’m using the TwentyTen theme to create a child theme, but I can’t seem to get rid of the ‘One column, no sidebar’ page template that is in the TwentyTen parent theme.

I thought just copying it over and deleting the contents would do the trick, but it seems not. Does anyone know how to do this? I’m sure it’s very simple.

Thanks

osu

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Overriding that template would be much easier than getting rid of it. Just the way logic goes.

I make no claim it’s efficient idea (late here), but this would get it nuked from edit screen:

add_action('admin_head-post.php','remove_template');

function remove_template() {

    global $wp_themes;

    get_themes();
    $templates = &$wp_themes['Twenty Ten']['Template Files'];
    $template = trailingslashit( TEMPLATEPATH ).'onecolumn-page.php';
    $key = array_search($template, $templates);
    unset( $templates[$key] );
}

Method 2

WordPress 3.9 introduces a theme_page_templates filter.

The example below from a Twenty Fourteen child theme functions.php shows how to remove the “Contributor Page” template:

function tfc_remove_page_templates( $templates ) {
    unset( $templates['page-templates/contributors.php'] );
    return $templates;
}
add_filter( 'theme_page_templates', 'tfc_remove_page_templates' );

Method 3

Expanding on @Rarst’s answer, here’s a more generic approach that is not tied to a specific theme, but can be used inside your own child theme’s functions.php to nuke any parent theme page templates you want to get rid of.

function remove_template( $files_to_delete = array() ){
    global $wp_themes;

    // As convenience, allow a single value to be used as a scalar without wrapping it in a useless array()
    if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );

    // remove TLA if it was provided
    $files_to_delete = preg_replace( "/.[^.]+$/", '', $files_to_delete );

    // Populate the global $wp_themes array
    get_themes();

    $current_theme_name = get_current_theme();

    // Note that we're taking a reference to $wp_themes so we can modify it in-place
    $template_files = &$wp_themes[$current_theme_name]['Template Files'];

    foreach ( $template_files as $file_path ){
        foreach( $files_to_delete as $file_name ){
            if ( preg_match( '//'.$file_name.'.[^.]+$/', $file_path ) ){
                $key = array_search( $file_path, $template_files );
                if ( $key ) unset ( $template_files[$key] );
            }
        }
    }
}

So you can use it in your child theme’s functions.php file like so:

add_action( 'admin_head-post.php', 'remove_parent_templates' );

function remove_parent_templates() {
    remove_template( array( "showcase.php", "sidebar-page" ) );
}

Here I’m just illustrating that you don’t have to pass the “.php” part if you don’t want to.

Or: remove_template( "sidebar-page" ); – you don’t need to pass an array if you want to modify only a single file.

Method 4

There’s a new filter in WP core (3.9) to remove page templates. It can be used from child themes.

Here’s how to achieve this in TwentyTen (tested on WP 3.9):

add_filter( 'theme_page_templates', 'my_remove_page_template' );
    function my_remove_page_template( $pages_templates ) {
    unset( $pages_templates['onecolumn-page.php'] );
    return $pages_templates;
}

https://core.trac.wordpress.org/changeset/27297

http://boiteaweb.fr/theme_page_templates-hook-semaine-16-8033.html

Method 5

Since previous answers no longer work here in current versions of WordPress and there was a related question which I just answered (April 2013) using a PHP output buffer I figured I’d post a link to that answer.

Also just published the Omit Parent Theme Page Templates plugin that filters out all parent theme page templates from the dropdown list of templates in the Page Attributes metabox when adding or editing a WordPress “Page.”

Method 6

July 10, 2012 – WordPress 3.4.1

Previous answers are not working and as Rarst said in a comment:

theme-related internals went through major refactoring and API changes in 3.4, so plenty of older stuff won’t work

Quick and Dirty jQuery Solution

add_action('admin_head', 'wpse_13671_script_enqueuer');

function wpse_13671_script_enqueuer() {
    global $current_screen;

    /**
     * /wp-admin/edit.php?post_type=page
     */
    if('edit-page' == $current_screen->id) 
    {
        ?>
        <script type="text/javascript">         
        jQuery(document).ready( function($) {
            $("a.editinline").live("click", function () {
                var ilc_qe_id = inlineEditPost.getId(this);
                setTimeout(function() {
                        $('#edit-'+ilc_qe_id+' select[name="page_template"] option[value="showcase.php"]').remove();  
                    }, 100);
            });

            $('#doaction, #doaction2').live("click", function () {
                setTimeout(function() {
                        $('#bulk-edit select[name="page_template"] option[value="showcase.php"]').remove();  
                    }, 100);
            });       
        });    
        </script>
    <?php
    }

    /**
     * /wp-admin/post.php?post=21&action=edit
     */
    if( 'page' == $current_screen->id ) 
    {
        ?>
        <script type="text/javascript">
        jQuery(document).ready( function($) {
            $('#page_template option[value="showcase.php"]').remove();
        });
        </script>
    <?php
    }
}

No Hook for That?

If I followed the correct path, this is where the “action” happens (/wp-includes/class-wp-theme.php), and looks like there’s nothing here to hook on

/**
 * Returns the theme's page templates.
 *
 * @since 3.4.0
 * @access public
 *
 * @return array Array of page templates, keyed by filename, with the value of the translated header name.
 */
public function get_page_templates() {
    // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
    if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
        return array();

    $page_templates = $this->cache_get( 'page_templates' );

    if ( ! is_array( $page_templates ) ) {
        $page_templates = array();

        $files = (array) $this->get_files( 'php', 1 );

        foreach ( $files as $file => $full_path ) {
            $headers = get_file_data( $full_path, array( 'Template Name' => 'Template Name' ) );
            if ( empty( $headers['Template Name'] ) )
                continue;
            $page_templates[ $file ] = $headers['Template Name'];
        }

        $this->cache_add( 'page_templates', $page_templates );
    }

    if ( $this->load_textdomain() ) {
        foreach ( $page_templates as &$page_template ) {
            $page_template = $this->translate_header( 'Template Name', $page_template );
        }
    }

    if ( $this->parent() )
        $page_templates += $this->parent()->get_page_templates();

    return $page_templates;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x