70

When you activate a wordpress theme, it's always a hassle to find out which file to go to change things. Any idea how to simplify things?

But on the other hand, considering the get_template_part functionality, this may be impossible. What do you say?

4
  • 1
    I inspect the html and find an identified tag or something unique. Commented Dec 27, 2011 at 18:38
  • 1
    View the source code and look for the body classes which tell you which template is used. Also gives you the i.d. Commented Feb 4, 2014 at 21:33
  • Possible duplicate of Get name of the current template file
    – Burgi
    Commented Jun 13, 2017 at 9:56
  • @BradDalton +1. Specially when we are not allowed to install a plugin or write a function to achieve the goal. Commented Jul 13, 2018 at 6:57

8 Answers 8

44

Hook onto template_include, set a global to note the template set by the theme then read that value back into the footer or header to see which template is being called for a given view.

I spoke about this filter hook before in Get name of the current template file, but go grab a copy of that code and plonk it your theme's functions.php file.

Then open up the theme's header.php or footer.php(or wherever you like) and use something like the following to print out the current template.

<div><strong>Current template:</strong> <?php get_current_template( true ); ?></div>

If you wanted to use this on a production site and keep that info away from your non-administrator users, add a little conditional logic.

<?php 
// If the current user can manage options(ie. an admin)
if( current_user_can( 'manage_options' ) ) 
    // Print the saved global 
    printf( '<div><strong>Current template:</strong> %s</div>', get_current_template() ); 
?>

Now you can keep track of what views are using what template, whilst keeping that info away from your visitors.

7
  • 1
    If there is something wrong with this answer, or if anyone could provide comments on what could be done to improve this answer, have at it, drop a comment here and share your thoughts and ideas on how to make it better.
    – t31os
    Commented Jan 28, 2014 at 16:03
  • 1
    It didn't work bro, it says "Undefined function" Commented Apr 27, 2016 at 15:48
  • 1
    @LucasB same here, that's the error I got Commented Jan 7, 2017 at 0:21
  • 2
    This should be get_page_template Commented Aug 11, 2017 at 20:57
  • 1
    To those who are too lazy to actually read, get_current_template is a CUSTOM function that @t31os proposes to use, just follow his links, to see how its done. get_page_template is useless, it is only good for actual pages, read the WP Doc for clarification. As far I know, the solution proposed here is the only holistic way to detect the current template, in all cases.
    – user75665
    Commented Aug 19, 2021 at 10:44
44

I use this handy function that displays the current template only for super admins:

function show_template() {
    if( is_super_admin() ){
        global $template;
        print_r($template);
    } 
}
add_action('wp_footer', 'show_template');

Hope that helps. :)

2
  • 4
    This is the goto answer, should be accepted. Commented Mar 13, 2018 at 0:21
  • 2
    I use this also but it still lacks the display of which “include” is being used and only shows the top level page.
    – Burndog
    Commented Jul 8, 2020 at 13:18
44

Well, if all you want is to check which template file has been used to generate the current page then you don't need to get your hands dirty with code ;)

There's this handy plugin called Debug Bar. It's a great helper in many situations including yours. You should definitely check it out - for me and many others it's a must-have companion for any WP development.

I've attached a screenshot that could make you fall in love...

enter image description here

To get the Debug Bar working, you need to enable wp_debug and wp_savequeries options. These options are in disabled state by default.

Before you make any changes though, there are a few points to keep in mind:

  • Do not do it in production environment unless the website doesn't cater to a lot of traffic.
  • Once you finish debugging, ensure to disable the options (especially the wp_savequeries option since it affects the performance) of the website.

To make the changes:

  1. Open wp_config.php file through a ftp client.
  2. Search for wp_debug option. Edit it to define( 'WP_DEBUG', true );. If the line is not present, add it to the file.
  3. Similarly, edit or add the line define( 'SAVEQUERIES', true ); to the file.
  4. Save. You are ready to debug.

More info: Codex

2
  • 2
    @justCallMeBiru -- the Debug Bar plugin doesn't require WP_DEBUG and SAVEQUERIES, though it is enhanced by them.
    – Pat J
    Commented Jan 15, 2014 at 16:35
  • 3
    Running such a plugin, just for one tid bit of information creates alot of overhead imho, and thus it is why i did not suggest it in my own answer. That said, clearly people prefer this answer, i'm curious to know why though.
    – t31os
    Commented Jan 28, 2014 at 16:02
12

Add the following code right after the get_header line in each relevant template file:

<!-- <?php echo basename( __FILE__ ); ?> -->

In your browser > view source, and the template name will be displayed as a comment in your html code, e.g.

<!-- page.php -->
4
  • it's too much effort to add this everywhere
    – Adal
    Commented Feb 18, 2019 at 1:49
  • hahaha, why bother with this if you're going to label each file then simply label it with its actual file name!
    – Aurovrata
    Commented May 9, 2020 at 6:29
  • @Aurovrata it was a long time ago. There are way better solutions. But I had a simple script to insert the code at the top of all files in a folder, so no hardcoding of actual names required. Done in 1 or 2 seconds.
    – ronald
    Commented May 20, 2020 at 18:46
  • fair enough, :)
    – Aurovrata
    Commented May 21, 2020 at 11:27
8

Here you go:

A HTML-list with all template files in use for the current landing page, including all template-parts from plugins, child theme and/ or parent theme combinations, all in one line of code:

echo '<ul><li>'.implode('</li><li>', str_replace(str_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(str_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php, but before admin bar renders.

if admin-bar stuff path is showing at the top, or any other file, change the filename template-loader.php in this line of code to: whatever filname you need to break from. Often: class-wp-admin-bar.php

if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:

add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);

priority -5 make shure it loads first. The key is to call get_included_files() at the right moment, otherwise some array-popping needed!

To break this up:

You can not collect all included template files without PHP backtrace. Superglobals inside template_include wont collect them all. The other way is to "place a marker" in each template file, but if you need to interact with the files first, you hazzle with time and the whole idea.

1) We need to check inside all the files that have been used by current Wordpress request. And they are many! Dont be surprised if you are using 300 files before even your functions.php is registered.

$included_files = str_replace('\\', '/', get_included_files());

We are using the PHP native get_included_files(), converting backslashes to forward slashes to match most of Wordpress returning paths.

2) We are cutting that array from where the template-loader.php is registered. After that, the populated get_included_files() should only have template files populated.

/* The magic point, we need to find its position in the array */
$path = str_replace('\\', '/', ABSPATH);
$key = $path.'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);

3) Shorten down the results, we dont need the path until theme folder or plugin folder, as templates in use, can be mixed from plugins, theme or child theme folders.

$replacement = $path.'wp-content/';
$output = str_replace($replacement, '', $output);

4) Finally, convert from array to a nice HTML list

$output = '<ul><li>'.implode('</li><li>', $output).'</li></ul>';

A last modification might be needed in part3) -replacement, if you dont want required includes by plugins. They might call class-files late, and "intercept" during the template output processing.

However, I found it reasonable to leave them visible, as the idea is to track whats been loaded, even if it is not a "template" that rendering output in this stage.

5

There's another more bare-bones plugin specifically for this purpose. I'm leaning towards installing the debug bar, because those other features look useful, but this one is more basic and specifically for this purpose: http://wordpress.org/extend/plugins/what-the-file/

1
  • Saved me some time.
    – m4n0
    Commented Dec 11, 2020 at 14:08
4

One very simple thing I do is to insert an HTML comment identifying the template file in each relevant file of the theme, eg at the top of index.php I have

<!-- index -->

and at the top of front-page.php

<!-- front -->

But obviously that requires modifying the theme. I suspect you could add a custom function in the footer.php file or header.php which would tell you what file was being used. The above method and the reference chart http://codex.wordpress.org/Template_Hierarchy are what I tend to use.

4

Easiest way I've found is to include the WordPress function on the body tag. It'll add several classes depending on which page you're viewing (home for the front, page for page, etc).

Check it out here: http://codex.wordpress.org/Function_Reference/body_class

Plus it's helpful for targeting elements with CSS on those pages.

Getting to know the Template Hierarchy (http://codex.wordpress.org/Template_Hierarchy) as David R mentioned is also a good idea.

Not the answer you're looking for? Browse other questions tagged or ask your own question.