How do I get the name of the file that is being used to render the current page?
Asked Answered
S

5

8

Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php.

I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.

I could do something with $_SERVER['PHP_SELF'], but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.

Selflove answered 23/2, 2012 at 1:34 Comment(0)
L
6

It can be printed in the Html source code like this:

add_action( 'wp_head', 'so_9405896_show_template', 999 );

function so_9405896_show_template() {
    global $template;
    echo '
    <!--

    TEMPLATE = ' . basename($template) .'

    -->
    ';
}

Or for easier visualization, directly in the content with this:

add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );

function so_9405896_the_content_filter( $content ) 
{
    if( is_admin() || !current_user_can( 'administrator' ) ) 
        return $content;

    global $template;
    $the_templ =  '<strong style="background-color: #CCC;padding:10px">TEMPLATE = ' 
                  . basename( $template ) . '</strong><br />';  

    $content = sprintf( $the_templ . '%s', $content );

    return $content;
}

Which results in:

output template name to content

Lizettelizotte answered 1/8, 2012 at 13:16 Comment(0)
P
0

As far as I've seen there's no built in option to enable such logging, only for errors.

I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.

I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.

Platysma answered 23/2, 2012 at 1:40 Comment(0)
S
0

A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.

Subside answered 23/2, 2012 at 10:45 Comment(0)
S
0

I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live. Simple and easy, if not so graceful.

Studbook answered 24/2, 2012 at 11:54 Comment(1)
So was I, I was wondering if there was a way to programmatically output it somewhere so I could find the page without having to go and open each file and type it in.Selflove
M
0

For those looking for a newer answer:

<?php if ( is_user_logged_in() ) { global $template; echo basename($template); } ?>

This checks if a user is logged in, then only shows the template name. This can be handy if you need to hack your way on a live site without adding functions.

Maturation answered 19/4, 2022 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.