Detect WordPress template file being used

This is a handy function which I tend to use from time to time when debugging my blogs, it outputs the name of the template file being used to display the current page.

Do not forget to comment out the function or even delete it before going live, or you’ll be giving out some rather useless information to your site visitors.

Just paste the following into your functions.php file:

The Reveal Template plugin does the same function, but is packaged as a plugin. You might want to check that out if you don’t feel like adding anything to your functions.php file.

About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Related Articles

  • WordPress Means Freedom

    As a WordPress user, you get four core freedoms due to the open source nature of WordPress’ software license. These WordPress freedoms are in contrast to closed-source desktop software like…

  • Thank You WordPress

    Share this graphic if WordPress has improved your life. I know it has given me a lot of opportunities, and I bet it has helped many of you too.

  • Using WordPress as a Development Platform

    Japheth Thomson presented on WordPress as a development platform at WordCamp Melbourne. You can check him out on his website and on Twitter at @japh.

13 Responses

  1. Meekohi
    Meekohi March 6, 2011 at 01:00 | | Reply

    Does not work.

  2. The Frosty
    The Frosty June 8, 2011 at 21:48 | | Reply

    Awesome, just what I needed.

  3. Thomas Ireland
    Thomas Ireland January 26, 2012 at 23:40 | | Reply

    Thanks Jean – had spent quite a few minutes banging my head trying to figure out why my template was not showing my changes… Your code helped me realise I was editing the wrong file. Doooh

    Thanks for the code snippet.

  4. Brian
    Brian February 7, 2012 at 05:32 | | Reply

    Thank you.

  5. brett
    brett February 12, 2012 at 05:07 | | Reply

    I had a quick idea, instead of this line:

    print_r($template);

    replace with:

    if(isset($_GET[‘template’])) {
    echo $template;
    }

    Now you can display the template path by adding a URL parameter. For example, yourwebsite.com/blog/?template=1. This is a simple way to display the path on call.

    Thoughts?

  6. felix
    felix September 28, 2012 at 10:40 | | Reply

    also a great idea is to add the display only if user admin

    add_action(‘wp_head’, ‘show_template’);
    function show_template() {
    global $template;

    global $current_user;
    get_currentuserinfo();
    if ($current_user->user_level == 10 ) print_r($template);
    }

  7. Tracy
    Tracy November 30, 2012 at 19:34 | | Reply

    Helped me out of a quagmire. Thank you.

  8. Arun
    Arun September 10, 2013 at 17:39 | | Reply

    Thanks!

  9. msk
    msk July 30, 2017 at 05:16 | | Reply

    add_action(‘wp_head’, ‘show_template’);
    function show_template() {
    global $template;
    print_r($template);
    }

Leave a Reply