post

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;
print_r($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.

Comments

  1. 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.

  2. Very nice…and handy. Saving this code now :)

  3. 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!

    • 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. 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 ) : '?';
    			get_currentuserinfo();
    			if ( !is_user_logged_in() )
    				return;
    			if ( is_admin_bar_showing() )
    				$wp_admin_bar->add_node( array(
    					'parent' => false,
    					'id' => 'template',
    					'title' => $title,
    					'href' => '#'
    				) );
    		}
    
  5. Hey John,

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

  6. Great post John!

  7. 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 says:

    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!

Trackbacks

  1. [...] to VegasGeek and Mark Gason for the code and inspiration for this plugin, [...]

Leave a Reply