Find out what template a WordPress theme is using.

Recently I was working on a clients website and it was decided that I was going to integrate buddypress on the website. I have worked with buddypress in the past and found it a great bit of software to extend upon with WordPress. This time round it was a bit more of a overall custom feel to it and some extra theming was required.

Some of the buddypress pages used different templates and I found it misleading sometimes as to which pages where using which templates. I found a great little bit of code that will show only the administrator who is logged in which template the current page is using. Great for debugging and other little things.

First of all we need to register a new little function, with which we will be using a global var. Put this in to your themes functions.php file.

[php]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’];
}[/php]

Once we have registered our new function, we will want to call it in our theme files somewhere. I simply added the below snippet to my header.php file of my theme. This code will print out the current page template by using the function we registered before.

[php]// If the current user can manage options(ie. an admin)
if( current_user_can( ‘manage_options’ ) )
// Print the saved global
printf( ‘

Current template: %s

‘, get_current_template() );
?>[/php]

So there you have it, if you ever get stuck needing to find out what page is using what template (great for plugins) you can use this.

Edit: I also found this little snippet and it can be used in the same manner:

[php]
add_action(‘wp_head’, ‘show_template’);
function show_template() {
global $template;
print_r($template);
}[/php]


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *