Print WordPress template filename(s) for debugging

I’ve taken over the maintainance of a large WP-MS powered site. The site has ~200 templates, many page specific, and with no standard naming procedure.

It would save a lot of time if while surfing around the site I can see the template names which make up the current page. Is this possible? I’ve looked at the debug plugins for WordPress, but they appear geared towards variables and SQL queries, not templates.

Related posts

Leave a Reply

4 comments

  1. I use this to simply print the template filename at the top of the page for debugging purposes.

    // For debugging - show template file
    add_action('wp_head', 'show_template');
    function show_template() {
        global $template;
        print_r($template);
    }
    
  2. The following functions do 3 things:

    • Show the template hierarchy for the current requesst
    • Show the theme in use (2 ways to acchive this shown)
    • Show the current template in use for the request *)

    *) Attach it to the content filter. You may need to play with the conditional or capability depending on your role. So far I don’t know a solution to show the page template for archives and similar list view requests.

    // That's not that easy to read:
    var_dump( get_required_files() );
    
    /** 
     * Show template hierarchy and theme at the end of the request/page.
     * @return void
     */
    function wpse31909_template_info()
    {
            // Don't display for non-admin users
            if ( ! current_user_can( 'manage_options' ) )
                return;
    
            // You have to build yourself the hierarchy here or somewhere in front of the fn
            global $wp_template_hierarchy;
    
            $content  = '<pre>';
                // Show template hierarchy
                $content .= "TEMPLATE HIERARCHYn==================n";
                $content .= var_export( $wp_template_hierarchy, true );
    
                // Show current theme in use:
                $content .= "nnCURRENT THEMEn=============n";
                $content .= var_export( get_option( 'template' ), true );
                // or: 
                # $content .= var_export( get_template(), true );
            $content .= '</pre>';
    
            return print $content;
    }
    add_action( 'shutdown', 'wpse31909_template_info' );
    
    
    /**
     * Show template on singular views attached to the end of the content for admins
     * @return $content
     */
    function wpse31909_template_to_content( $content )
    {
            // Display standard content for non-admin users and not single post/page/cpt/attachment view.
            if ( ! current_user_can( 'manage_options' ) && ! is_singular() )
                return $content;
    
            $content .= '<pre>';
                // Show current template in use: Must be in the loop to get the global $post
               $content .= var_export( get_post_meta( $GLOBALS['post']->ID, '_wp_page_template' ), true );
            $content .= '</pre>';
    
            return $content;
    }
    add_filter( 'the_content', 'wpse31909_template_to_content' );
    
  3. While not directly an answer to the question at hand, it should probably also be noted here that by default WordPress adds a class name to the <body> tag for custom page templates. So for instance, if you are viewing a page that has a custom page template, and the template’s filename is custom.php, then a class name of page-template-custom will be added to the <body> tag.

    Documentation on this feature can be found here:
    https://codex.wordpress.org/Function_Reference/body_class#Page