Get name of the current template file

  • I've found this to display the current name of the file used in template:

    function get_template_name () {
        foreach ( debug_backtrace() as $called_file ) {
            foreach ( $called_file as $index ) {
                if ( !is_array($index[0]) AND strstr($index[0],'/themes/') AND !strstr($index[0],'footer.php') ) {
                    $template_file = $index[0] ;
                }
            }
        }
        $template_contents = file_get_contents($template_file) ;
        preg_match_all("Template Name:(.*)\n)siU",$template_contents,$template_name);
        $template_name = trim($template_name[1][0]);
        if ( !$template_name ) { $template_name = '(default)' ; }
        $template_file = array_pop(explode('/themes/', basename($template_file)));
        return $template_file . ' > '. $template_name ;
    }
    

    Source: get name of page template on a page

    It works quite well, except that in the backend, in the template select box, I get this ugly extra entry:

    screenshot

    Does anybody have any idea how to fix it? I don't even know why this function is called in the backend. Is there a conditional function like is_frontend() - maybe this would solve the problem?

    @chodorowicz - While I will stop one step short of calling the selection of `functions.php` as a bug, I will agree completely with your premise. To make matters worse I scanned the WordPress core code and found about 5 places where there could have been a hook to allow you to handle this issue yet I found none. I'd suggest posting a ticket on http://core.trac.wordpress.org.

    @MikeSchinkel - thanks for comment, but doesn't `template_include` hook, which t31os suggested, solve the issue? Or maybe I've misunderstood you.

    @chodorowicz - You now have me very confused. The answer @t31os gave solves a problem that is very different from the one that I understood you were asking based on your question and your follow up comments to other's answers. But if @t31os' answer solved your actual need, then my confusion here is unimportant as your question has been answered.

    Yes, the actual need was to display the name of the currently used theme file, the code I posted had a flaw (which can be solved by moving the function out of the main theme folder), but @t31os' solution is much cleaner and fulfills the need, but, if I understand you correctly, the real problem (WP shouldn't try to read `functions.php` as page template file) still persists. I'll try to post it on trac, haven't done it yet. Greetings!

    @chodorowicz - Yes, my comment was addressing the real problem, keeping WP from trying to read `functions.php` and/or allowing you to write a hook to change was is returned. Glad to see you found a patch rather than duplicate it; it's so hard to find those existing tickets.

    I've made a new plugin in order to display the current template. Check it on http://wordpress.org/extend/plugins/display-template-name/

    ^ ie. you took the code from my answer and wrapped it into a plugin. And you did all that without providing any credit to the origin, neither myself or the WP stackexchange... nice... :/

  • apparently this is enough:

    add_action('wp_head', 'show_template');
    function show_template() {
        global $template;
        echo basename($template);
    }
    

    or just use it directly in template (I tend to echo in footer.php in HTML comment)

    <?php global $template; echo basename($template); ?>
    

    That won't work with get-template-part just so you know, it only shows single.php (for example) and not the file it is in.

    Yes, it's true. To get the name of included file you'd probably need to use something like this `echo __FILE__;`

    this is fine, for example in cases when you modify the default template without assigning it to a post in backoffice. For example using custom routes and the template_include filter. Thank you.

    How could I do this within a loop? I'm trying to output the URL to one page of each template file.

    @JacobTheDev maybe using `echo __FILE__` - because this won't work, it displays only main, initial template

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM