• scott

    (@scott-kobayashi-maru)


    function remove_template( $files_to_delete = array() ){
        global $wp_themes;
        if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );
        $files_to_delete = preg_replace( "/\.[^.]+$/", '', $files_to_delete );
        get_themes();
        $current_theme_name = get_current_theme();
        $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] );
                }
            }
        }
    }
    
    add_action( 'admin_head', 'remove_parent_templates' );
    
    function remove_parent_templates() {
        remove_template( array( "showcase.php", "sidebar-page.php" ) );
    }

    This code used to nicely remove parent theme templates from the array of templates available to a child theme. Since the 3.4 update, this no longer works. If you look at the array of templates, this code does remove them, none of these functions have been deprecated, however the way WP uses $wp_themes must be different.

    If you create/edit a page, in the drop down list of template options you will see your child theme templates and parent theme templates, even though this code should have removed those parent templates.

    Does anyone know of a solution to this, other than physically removing the parent theme templates?

Viewing 5 replies - 1 through 5 (of 5 total)
  • pending a better solution, I use the following code :

    $_templates_to_remove = array();
    
    function remove_template( $files_to_delete = array() ){
        if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );
    
    	global $_templates_to_remove;
    	$_templates_to_remove = array_unique(array_merge($_templates_to_remove, $files_to_delete));
    
    	add_action('admin_print_footer_scripts', '_remove_template_footer_scripts');
    }
    
    function _remove_template_footer_scripts() {
    	global $_templates_to_remove;
    
    	if ( ! $_templates_to_remove ) { return; }
    	?>
    	<script type="text/javascript">
    	jQuery(function($) {
    		var tpls = <?php echo json_encode($_templates_to_remove); ?>;
    		$.each(tpls, function(i, tpl) {
    			$('select[name="page_template"] option[value="'+ tpl +'"]').remove();
    		});
    	});
    	</script>
    	<?php
    }
    
    // Usage
    add_action('admin_head-post.php', 'remove_parent_templates');
    add_action('admin_head-post-new.php', 'remove_parent_templates');
    
    function remove_parent_templates() {
    	remove_template(array(
    		'page-archives.php',
    		'page-authors.php',
    	));
    }
    Thread Starter scott

    (@scott-kobayashi-maru)

    Thanks! This worked perfectly.

    I wish I knew what changed in WP 3.4.1 that caused my code to stop working. I’ll investigate later. I totally appreciate this code! and hope others with this problem will benefit from your post.

    Scott.

    Could you provide more guidance on how to implement? Simply adding the above code to my child functions.php and updating the file names in the array does not seem to work. Am I missing something? I’m running WP 3.4.2…

    Perfect! I can confirm this still works in WP 3.5.1

    Works great, pretty straight forward implementation
    also confirming it on WordPress 3.5.1.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘remove parent theme templates post 3.4’ is closed to new replies.