Development Tip: How to Know What Template is Being Used

I’ve been working on an overhaul of my personal blog for a while now. My old theme was pretty bloated, and way more than I needed since I offloaded everything away. The site used to have everything on it, but I moved all my WordPress posts to VoodooPress, and all my Voodoo Empire specific stuff to its own site. My blog doesn’t need all the features it had, and it was slowing things down. I haven’t had time to finish my custom theme, so in the meantime I decided to play around with some available themes, starting with Fanwood from DevPress. I have all of my custom stuff that used to be in the theme living in its own functionality plugin now. So I can activate any new theme, and within 20 minutes, have all my custom stuff in place. But the one problem I ran into – FanWood is a complex theme, not always using the standard WordPress templates I would expect – single.php for instance. Well, what’s an easy way to know what template is being loaded at all times? I’ve got the answer for ya!

I found this after a fair amount of Google-fu. I believe this solution came from @t31os on the Stackexchange WordPress site. We can place a nifty function into our theme and always see what template is being loaded. First up, we need to build our function. You can drop this into functions.php of a child theme, or the parent theme if you are just testing a theme and don’t want to build a child. I normally don’t recommend putting anything into a parent theme, but often you will only use this temporarily to see how a theme works.

I’ll be honest here, even though I would consider this a theme specific tweak, I put it in my functionality plugin. The main reason was that I plan on hopping around a few different themes over the next while. By putting this into my plugin, all I need to do is call it within each theme, and I’m ready to test. Just remember, if you put this into a plugin, you need to wrap the next call to get_current_template in a function_exists check. That way if you deactivate the plugin with the call still in your theme, your whole site doesn’t crash. Now, here’s the function:

add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
     $GLOBALS['current_theme_template'] = basename($t);
     return $t;
}

function get_current_template( $echo = false ) {
     if( !isset( $GLOBALS['current_theme_template'] ) )
         return false;
     if( $echo )
         echo $GLOBALS['current_theme_template'];
     else
         return $GLOBALS['current_theme_template'];
}

This is the function to grab the current displayed template. It grabs it, and hangs on tight, ready to display it to you at a moments notice! However, we need to actually ask for it! So the next thing we need to do is call it from somewhere. The obvious place is going to be header.php. The reasoning is simple here, header.php always loads! So if you put it there, you only need to add it once, and you can see any template that loads. So here is what you need!

<!-- TEMPLATE USED -->
<div class="TEST <?php echo get_current_template(); ?>"></div>

Now, there are a variety of ways to do this. I’m not actually sure why I assigned it to a class. Just what popped into mind. You could also use it directly inside of a comment I’m sure. I put this right after the opening body tag, that way I knew exactly where to find it easily. Outputting it as a class name or inside of a comment keeps the output hidden, so you can only see it in the source code. You could also directly output it within your theme if you don’t feel like viewing your source code during testing, that’s all up to you!

Hopefully this helps some of you out a bit. I know it’s going to be a very useful feature for me during my testing of various themes. It’s hard to figure out exactly where to plop some of my functions sometimes, especially as WordPress themes grow more and more complex.

13 Replies to “Development Tip: How to Know What Template is Being Used”

    1. Nope… not exactly. The body class will let you know you are on a page, a single column, a single view, etc. But on a customized theme, like the Fanwood one I’ve been testing, things are very different. There are about 50 templates in this thing, and none are really WP standard, so I’ve got things like home.php, which is used rather than index, I’ve got post.php instead of single, then I have a variety of templates that could load depending on selected layout, format, etc. So this gives me the exact whatever.php filename of the currently loaded template. For instance on this post: http://vudu.me/352
      I get

      <body class="wordpress ltr en_US child-theme y2012 m03 d28 h11 wednesday logged-in admin-bar singular singular-post singular-post-31998 post-format-standard custom-background layout-default">
      <div class=".....>
      

      But the actual output from my code above is:

      <div class="TEST post.php">
      </div>
      
      1. When I am using custom templates they actually do show up in my body class tag, however I must say that a “standard” template like home.php does not show up. I think it is a very good idea to output your code in the body class, that would also help to make changes in the CSS depending on templates (as per Sean’s suggestion below).

        1. Custom page templates would be indicated in the body class. The templates I have aren;t page templates… They are about 50 custom templates for all aspects of WP… I haven’t even totally figured out the logic behind them all yet. Outputting my code into the body class is a simple thing… except one problem. My code actually outputs the exact name of the template (post.php for example), I’d need to strip that .php off of it to use it (or at least the dot), because I’m pretty sure post.php wouldn’t work as a selector for css due to the dot. If I could just get the dot out so I’m left with postphp, that might be cool.

  1. Very nice!

    Great for developers, I tended to just create the template file as bare bones without the content call to ensure it was working before, but this is informative. I dont see how it could be used in css though. You class has a .php so wouldnt that conflict?

    1. This is more useful when you are grabbing someone else’s theme and have no idea how it works. More and more themes are getting super complex, and not sticking to the standard WP stuff we are used to (single.php, index.php, page.php, etc). So this just let’s me immediately figure the templates out… especially for themes which allow various content formats, post formats, etc, all of which offload to various templates.

      As for using it in css…. I’m not actually outputting it to be used as a selector. Just for information, but yeah, I wouldn’t think it could be used for styling.

  2. Very helpful for exactly the use you describe, getting a handle on which templates are being used in third party themes. Good of you to share this code.

Leave a Reply to bobbi Cancel reply