Created
December 20, 2021 16:18
-
-
Save broskees/03ad67980456b3cc9e28981abd0592b9 to your computer and use it in GitHub Desktop.
Add a Page Template column to the WordPress admin "Pages" page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Introduces a new column to the 'Page' dashboard that will be used to render the page template | |
* for the given page. | |
* | |
* @param array $page_columns The array of columns rendering page meta data. | |
* | |
* @return array $page_columns The update array of page columns. | |
*/ | |
add_filter('manage_edit-page_columns', function ($page_columns) { | |
$page_columns['template'] = 'Page Template'; | |
return $page_columns; | |
}); | |
/** | |
* Renders the name of the template applied to the current page. Will use 'Default' if no | |
* template is used, but will use the friendly name of the template if one is applied. | |
* | |
* @param string $column_name The name of the column being rendered. | |
*/ | |
add_action('manage_page_posts_custom_column', function ($column_name) { | |
// If we're not looking at the 'Template' column, then we're done. | |
if ('template' !== $column_name) { | |
return; | |
} | |
// Grab a reference to the post that's currently being rendered. | |
global $post; | |
// First, the get name of the template. | |
$template_name = get_page_template_slug($post->ID); | |
$template = untrailingslashit(get_stylesheet_directory()) . '/' . $template_name; | |
/** | |
* If the file name is empty or the template file doesn't exist (because, say, | |
* meta data is left from a previous theme). | |
* Otherwise, let's actually get the friendly name of the file rather than the name of the file itself | |
* by using the WordPress `get_file_description` function | |
*/ | |
$template_name = ( 0 === strlen(trim($template_name)) || ! file_exists($template) ) ? 'Default' : get_file_description($template); | |
// Finally, render the template name. | |
echo esc_html($template_name); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment