Press enter to see results or esc to cancel.

Determine Page Template In WordPress Admin

I needed to determine the page template used by a page while in edit mode in WordPress admin in order to show or hide a metabox specifically needed for pages that use that template. It took me a while to find a solution but I finally did thanks to help from this forum post.

The page template used by a WordPress page is stored as a custom field value of that page in wp_postmeta table under the meta_key ‘_wp_page_template’. The underscore in _wp_page_template makes sure the field doesn’t show up in the custom fields section of the page. By default, a WordPress page has the value ‘default’ stored against ‘_wp_page_template’ meta key.

Here is the code you can use to determine the value of ‘_wp_page_template’:

global $post;
$template_file = get_post_meta($post->ID,'_wp_page_template',true);

if ($template_file = 'my-page-template.php')
     add_meta_box... or do whatever.
Comments

Leave a Comment