Get which template being in use within WordPress-admin

I want to get the template in the theme which being in use of the current edited post(_type)/Page.

Lets say im editing a single Post, Then i want to retrive that it using the template single.php

Read More

And if im editing a Custom Post Type that have its own single-{post_type}.php i want to retrive that.

How can i archive that? are there any global variables that store this information?

I know that pages saves it as meta_data.

I dont have mutch to go with but i have been looking att get_query_template and locate_template but i need something in the right direction.

Thanks.

Related posts

Leave a Reply

2 comments

  1. Basically this isn’t available in admin. You can take a look at my profile, go to my github page and grab the “current admin info” plugin. But this won’t help you much.

    This would show you the template name:

    get_page_template_slug( get_queried_object_id() );
    

    On the front end you can use the following plugin, that will display that info (it’s from another question).

    <?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( 'wp_footer', array( $this, 'get_template_name' ), 100 );
        }
    
        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 printf(
                     '<div><p style="text-align:center;">%s</p></div>'
                    ,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 );
    
            // Subdirectory info
            $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;
    

    If all this doesn’t help as starting point, then please take a look at all the related answers.

  2. This answer was originally an edit of the OP to my other answer. I’ll correct the code later on.

    So i ended up with a pretty simple solution. This answer did get me in the right direction.

    I don’t now if this is the best way to do it but it fills my requirements:

    if ( $pagenow == 'post.php' )
    {
    
        $template = get_post_meta( $post->ID, '_wp_page_template', true );
    
        // Check page-templates, if not set -> page.php
        if ( $post->post_type == 'page' && ( empty( $template ) || 'default' == $template ) )
        {
            $template = 'page.php';
        }
        elseif( empty( $template ) )
        {
            // Check single-templates, single-{post_type}.php -> single.php order
            if ( file_exists( TEMPLATEPATH .'/single-'. $post->post_type .'.php' ) ) {
                $template = 'single-'. $post->post_type .'.php';
            }
            elseif ( ( $post->post_type == 'attachment' || $post->post_type == 'page' ) && file_exists( TEMPLATEPATH .'/'. $post->post_type .'.php' ) )
            {
                $template = $post->post_type .'.php';
            }
            elseif( $post->post_type == 'attachment' )
            {
                $template = '';
            }
            else
            {
                $template = 'single.php';
            }
        }
    
        // Read the file
        $file = implode( '', file( TEMPLATEPATH .'/' . $template ) );
    
        // Get the tags in the template, (Blocks Areas: Left, Right)
        if ( preg_match_all( '|Block Areas:(.*)$|mi', $file, $name ) )
        {
            $defined_areas = explode( ',', strtolower( $name[1][0] ) );
            $defined_areas = array_map( 'trim', $defined_areas);
        }
    }