Finding out which Wordpress template is used for a page from admin page
Asked Answered
I

3

13

I'm trying to retrieve the filename/path of template used on the 'Edit Page'-page in the Dashboard.

Similar to what wp-includes/template-loader.php (source) does on the front end: finding out which template to render.

Unfortunately, expressions like is_front_page() - which Wordpress' template-loader.php uses to find out if it should use get_front_page_template() - don't work correctly on the admin page. Which is to be expected because those expression use the global $wp_query object, and not the current query.

What I've tried so far:

Running a post loop inside the admin page

$args = array(
    'p' => get_the_ID(),
    'post_type' => 'any'
);

$query = new \WP_Query($args);

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    <?= the_title(); ?><br>
    Is front page: <?= is_front_page() ? 'true' : 'false' ?>

<?php endwhile; endif; ?>

Displays:

Home

Is front page: false

Using get_post_meta

<?= get_post_meta(get_the_ID(), '_wp_page_template', true); ?>

Displays:

default

...which would be the same for front-page.php on Home and page.php on another default page, so this doesn't help me.

In short

What I'm trying to get is front-page.php when I'm editing my 'Home' page. Or custom-template.php when I'm editing some page with the custom template selected. Or about-page.php when I'm editing a page called 'About'. How to get the correct filename or path?

Indoor answered 30/8, 2017 at 16:29 Comment(4)
I've found a solution to this problem by adding a meta tag containing the template filename on the front-end and using php's get_meta_tags from the backend to get the correct filename. Although this works, I'm wondering if there's a more elegant solution. Starting a bounty now.Indoor
how are you running the code attempt above? Via a hook?Amazed
In short: the meta tag is inserted using the wp_head hook. Using a similar function to this one the correct template path is loaded into the meta tag. Then, using curl or get_meta_tags (tried them both, both worked), I read that meta tag from the backend. It works, but it feels like a dirty solution.Indoor
A week later: thanks for the replies, even though I didn't find a better solution to my problem. I still can't get the REAL template file that is loaded when I'm on the admin page, unless using the curl trick to load the page on the front-end described in the comment above, which I'll keep using for now.Indoor
B
3

If your specific problem is with the home page, you could use a combination of get_page_template() and comparing the edited page's ID with get_option('page_on_front') (see WordPress option reference). There's also an option, show_on_front, which indicates whether the front page shows posts or a static page.

Maybe this helps? I don't know if there are other edge cases where a different template will be used...

Bermejo answered 11/9, 2017 at 8:50 Comment(2)
Thanks, this would indeed help for the front page. Unfortunately I would consider all template hierarchy cases edge cases, which are a lot. As I'm writing a plugin which should work for different themes I can't just incorporate some of the edge cases I need.Indoor
Yep, you could probably cobble something together checking for the presence of possible template files one-by-one, taking child themes etc. into account... which seems like it would get brittle quite fast, your hack with curl is probably the more practical solution!Bermejo
C
2

Use get_page_template():

<?php echo realpath(get_page_template()); ?> 

It outputs something like /foo/bar/baz/wp-content/themes/your-theme/{page-template}.php

You can choose not to use realpath() and just get the template name.

Crying answered 7/9, 2017 at 17:38 Comment(4)
This returns the page template set to the page, not that template that's actually loaded. For example, in the dashboard when I'm editing the homepage it returns path/to/template/page.php while it actually loads path/to/template/front-page.php, because Wordpress gives preference to that.Indoor
This function returns the template assigned to the current page. Period. If you're getting "default" or page.php, then you haven't set a custom template in the page edit screen... OR, it isn't saving to the db. Wordpress loads the template assigned to the page, and if there is none, it follows its own defaults based on template hierarchy. If this function isn't giving you what you want to see, or some other template is loading instead of the one this function gives you, then you need to reexamine your templates and declarations.Crying
Exactly, Wordpress follows Template Hierarchy. Which isn't reflected in the results of get_page_template(). I get page.php when I echo your function in the file front-page.php. Tested on two different themes.Indoor
And what is the output of get_page_template() if you use it on a page with a custom template? For instance, I'm working on an installation now that has a custom front page template, and get_page_template() outputs the proper template filename. I'd be willing to bet that if you have a front-page.php, and this function still shows page.php, that something is wrong with the front page settings.Crying
M
0

The only solution I found is this:

global $template;
echo basename($template); // front-page.php

I know it's ugly, but I just couldn't find a way to not use this global variable.

Magnien answered 28/11, 2019 at 21:47 Comment(1)
Thanks for your suggestion! Problem is not relevant for me anymore so didn't test it out.Indoor

© 2022 - 2024 — McMap. All rights reserved.