Skip to main content
added 1148 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

What the plugin tells you:

  • Is the template from the parent of child/current theme?
  • Is the template served from a subfolder? If yes: Tells you the name
  • The template file name.

Just copy the following code into a file and name it wpse10537_template_info.php, upload it to your plugins directory and activate it.

<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */

if ( ! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'is_parent_template' ), 0 );
        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'shutdown', array( $this, 'get_template_name' ) );
    }
    
    public function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $this->stack['file']>to_stack(
 =                "Template file"
                ,basename( $file )
            );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
        )
            return print implode( " &ndash; ", $this->stack );
    }

    public function get_template_file()
    {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $this->stack['slug']$slug = get_page_template_slug( get_queried_object_id() );
        if ( ! strstr( $slug, "/" ) )
            return $this->to_stack( "Template", $slug );

        $this->to_stack(
             "Subdirectory"
            ,strstr( $slug, "/", true )
        );

        $this->to_stack(
             "Template (in subdirectory)"
            ,str_replace( "/", "", strstr( $slug, "/" ) )
        );
    }

    public function is_parent_template()
    {
        if ( ! is_null( wp_get_theme()->parent ) )
            return $this->to_stack( 'from parent theme' );

        $this->to_stack( 'from current/child theme' );
    }

    public function to_stack( $part, $item = '' )
    {
        $this->stack[] = "{$part}: {$item}";
    }
} // END Class wpse10537_template_name

} // endif;
<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */

if ( ! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'shutdown', array( $this, 'get_template_name' ) );
    }
    
    function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $this->stack['file'] = basename( $file );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
        )
            return print implode( " &ndash; ", $this->stack );
    }

    public function get_template_file()
    {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $this->stack['slug'] = get_page_template_slug( get_queried_object_id() );
    }
} // END Class wpse10537_template_name

} // endif;

What the plugin tells you:

  • Is the template from the parent of child/current theme?
  • Is the template served from a subfolder? If yes: Tells you the name
  • The template file name.

Just copy the following code into a file and name it wpse10537_template_info.php, upload it to your plugins directory and activate it.

<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */

if ( ! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'is_parent_template' ), 0 );
        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'shutdown', array( $this, 'get_template_name' ) );
    }
    
    public function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $this->to_stack(
                 "Template file"
                ,basename( $file )
            );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
        )
            return print implode( " &ndash; ", $this->stack );
    }

    public function get_template_file()
    {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $slug = get_page_template_slug( get_queried_object_id() );
        if ( ! strstr( $slug, "/" ) )
            return $this->to_stack( "Template", $slug );

        $this->to_stack(
             "Subdirectory"
            ,strstr( $slug, "/", true )
        );

        $this->to_stack(
             "Template (in subdirectory)"
            ,str_replace( "/", "", strstr( $slug, "/" ) )
        );
    }

    public function is_parent_template()
    {
        if ( ! is_null( wp_get_theme()->parent ) )
            return $this->to_stack( 'from parent theme' );

        $this->to_stack( 'from current/child theme' );
    }

    public function to_stack( $part, $item = '' )
    {
        $this->stack[] = "{$part}: {$item}";
    }
} // END Class wpse10537_template_name

} // endif;
added 1161 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

Edit: Here's the new version of the plugin wrapped up in a class. It shows both the current template file name, as well as the template hierarchy file name in the shutdown hook at the most bottom of the page.

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: (#10537) »kaiser« Get Template file name */
function
if wpse10537_get_template_name( $file! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $name;$instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'shutdown', array( $this, 'get_template_name' ) );
    }
    
    function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $name$this->stack['file'] = basename( $file );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
        )
            return print $name;implode( " &ndash; ", $this->stack );
    }
add_action
    public function get_template_file()
 'template_include', 'wpse10537_get_template_name'  {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $this->stack['slug'] = get_page_template_slug( get_queried_object_id() );
    }
} // END Class wpse10537_template_name

} // endif;

This plugin can run as MU-Plugin too.

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: Get Template file name */
function wpse10537_get_template_name( $file )
{
    static $name;
    if ( 'template_include' === current_filter() )
    {
            $name = basename( $file );
        return $file;
    }

    // Return static var on echo call outside of filter
    if (
        current_user_can( 'manage_options' )
        AND defined( 'WP_DEBUG' )
        AND WP_DEBUG 
    )
        return print $name;
}
add_action( 'template_include', 'wpse10537_get_template_name' );

This plugin can run as MU-Plugin.

Edit: Here's the new version of the plugin wrapped up in a class. It shows both the current template file name, as well as the template hierarchy file name in the shutdown hook at the most bottom of the page.

<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */

if ( ! class_exists( 'wpse10537_template_name' ) )
{
    add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );

class wpse10537_template_name
{
    protected static $instance;

    public $stack;

    public static function init()
    {
        is_null( self :: $instance ) AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        if ( is_admin() )
            return;

        add_action( 'wp', array( $this, 'get_template_file' ) );
        add_action( 'template_include', array( $this, 'get_template_name' ) );
        add_action( 'shutdown', array( $this, 'get_template_name' ) );
    }
    
    function get_template_name( $file )
    {
        if ( 'template_include' === current_filter() )
        {
            $this->stack['file'] = basename( $file );
            return $file;
        }

        // Return static var on echo call outside of filter
        if (
            current_user_can( 'manage_options' )
            AND defined( 'WP_DEBUG' )
            AND WP_DEBUG 
        )
            return print implode( " &ndash; ", $this->stack );
    }

    public function get_template_file()
    {
        if ( ! is_post_type_hierarchical( get_post_type() ) )
            return;

        $this->stack['slug'] = get_page_template_slug( get_queried_object_id() );
    }
} // END Class wpse10537_template_name

} // endif;

This plugin can run as MU-Plugin too.

added 80 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

An addition (more sweet code) to other answers here.

Template Name

To just get the current page template name, use the following line.

is_page() AND print get_page_template_slug( get_queried_object_id() );

File Name

When you just want to echo the current template file name got, go with the following

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: Get Template file name */
function wpse10537_get_template_name( $file )
{
    static $name;
    if ( isset'template_include' === current_filter() )
    {
            $name )= basename( $file );
        return $name;$file;
    }

    // SetReturn static var on echo call outside of filter
    $nameif =(
 basename       current_user_can( $file'manage_options' );
        AND defined( 'WP_DEBUG' )
        AND WP_DEBUG 
    )
        return $file;print $name;
}
add_action( 'template_redirect''template_include', 'wpse10537_get_template_name' );

This plugin can run as MU-Plugin.

You can then simply call echo wpse10537_get_template_name() at any point (in for example a theme template). This avoids cluttering the global namespace.

An addition (more sweet code) to other answers here.

Template Name

To just get the current page template name, use the following line.

is_page() AND print get_page_template_slug( get_queried_object_id() );

File Name

When you just want to echo the current template file name got with the following

/** Plugin Name: Get Template file name */
function wpse10537_get_template_name( $file )
{
    static $name;
    if ( isset ( $name ) )
        return $name;

    // Set static var
    $name = basename( $file );

    return $file;
}
add_action( 'template_redirect', 'wpse10537_get_template_name' );

This plugin can run as MU-Plugin.

You can then simply echo wpse10537_get_template_name() at any point (in for example a theme template). This avoids cluttering the global namespace.

An addition (more sweet code) to other answers here.

Template Name

To just get the current page template name, use the following line.

is_page() AND print get_page_template_slug( get_queried_object_id() );

File Name

When you just want to echo the current template file name, go with the following

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: Get Template file name */
function wpse10537_get_template_name( $file )
{
    static $name;
    if ( 'template_include' === current_filter() )
    {
            $name = basename( $file );
        return $file;
    }

    // Return static var on echo call outside of filter
    if (
        current_user_can( 'manage_options' )
        AND defined( 'WP_DEBUG' )
        AND WP_DEBUG 
    )
        return print $name;
}
add_action( 'template_include', 'wpse10537_get_template_name' );

This plugin can run as MU-Plugin.

You can then simply call wpse10537_get_template_name() at any point (in for example a theme template). This avoids cluttering the global namespace.

added 381 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading
added 12 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading
added 339 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading