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?
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. – Indoorwp_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