WordPress Tip: Which template is being used?

Whenever I'm working on a new WordPress theme, especially one that uses different templates for Custom Post Types, archives, tags or any other string of possible data types, there always comes a time when I'm just not certain exactly which template WordPress is displaying.

If you aren't sure how the WordPress theme hierarchy works, this graphic shows the exact flow on how WordPress chooses which template file to use for a given page. But, when you are making changes to CPT slugs, page names, taxonomies and the like, in order to know exactly which file is actually being used on the page you're viewing, you're going to need a little more assistance. That's where this piece of code comes in to play.

Open up your theme's functions.php file and add the following:

add_action('wp_head', 'show_template'); 

function show_template() { 	
global $template;

Save the file and now when you view your site in a browser, you'll see something like this:

Just below the admin bar and right above the beginning of your site you'll see the directory path and file name being used on the page you're viewing. No more guessing!

It should go without saying, but I'll say it anyway… Unless you want this showing up on your live site, please comment out this code before pushing your theme live.

Posted in ,


  1. Jonathan on February 15, 2012 at 4:09 pm

    You could use the hook “admin_bar_menu” instead of “wp_head” to tie the template string into the Admin Bar, that way you wouldn’t need to worry about whether or not someone was logged in.

    • John on February 15, 2012 at 6:08 pm

      Hey now! that’s a great idea. Thanks!

  2. shayne on February 15, 2012 at 6:10 pm

    Very nice…and handy. Saving this code now 🙂

  3. TJ List on February 15, 2012 at 6:40 pm

    Do you know how many times I have manually hacked a theme template file to figure…

    Never mind, of course you do. That’s why you came up with a more awesome way. Thanks!

    • John on February 15, 2012 at 9:21 pm

      I absolutely know… What’s worse, I had this code in an Instant Message history file. So, every time I need to use it, I have to remember who I was talking to, then search the IM file… That’s why I finally just posted the damn thing!

  4. The Frosty on February 16, 2012 at 8:38 am

    This is what I use:

    add_action( 'wp_footer', 'show_template' );
    		function show_template() {
    			global $template, $wp_admin_bar;
    			$title = ( isset( $template ) ) ? str_replace( get_template_directory(), '', $template ) : '?';
    			if ( !is_user_logged_in() )
    			if ( is_admin_bar_showing() )
    				$wp_admin_bar->add_node( array(
    					'parent' => false,
    					'id' => 'template',
    					'title' => $title,
    					'href' => '#'
    				) );
  5. Quint on February 21, 2012 at 12:32 pm

    Hey John,

    Thanks! How would I get this to work for Buddypress pages?

  6. Cody Landefeld on March 28, 2012 at 11:44 am

    Great post John!

  7. Kraft on March 28, 2012 at 11:46 am

    Just came across your site and can’t tell you how many times I fiddled with a template to realize I was working on the wrong file.

    Thanks for posting this. So simple, yet so easy to overlook.

  8. Jonathan on March 29, 2012 at 2:07 pm

    Hi John,
    Got an email notice about the reply above me but it looks like you’ve changed your URL structure so the link was broken; might want to look into a 301 redirect.

    It currently points to ‘/2012/02/15/which-template-is-being-used’ but it looks like you’re no longer opting to use dates in your permalinks. Just a heads up!

  9. Kenny Eliason on July 14, 2014 at 11:15 am

    Man this is a great tool, it’s a default install on all my sites now!

Leave a Comment