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?
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
orfooter.php
(or wherever you like) and use something like the following to print out the current template.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.
Now you can keep track of what views are using what template, whilst keeping that info away from your visitors.
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…
To get the Debug Bar working, you need to enable
wp_debug
andwp_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:
To make the changes:
wp_config.php
file through a ftp client.wp_debug
option. Edit it todefine( 'WP_DEBUG', true );
. If the line is not present, add it to the file.define( 'SAVEQUERIES', true );
to the file.More info: Codex
I use this handy function that displays the current template only for super admins:
Hope that helps. 🙂
Add the following code right after the get_header line in each relevant template file:
In your browser > view source, and the template name will be displayed as a comment in your html code, e.g.
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:
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 filenametemplate-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:
priority
-5
make shure it loads first. The key is to callget_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.
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.
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.
4) Finally, convert from array to a nice HTML list
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.
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/
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
and at the top of front-page.php
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.
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.
There is a plugin named Theme Check which does exactly this. It displays the name of the current template file in use as a HTML comment.