Last active
August 29, 2015 14:02
-
-
Save chriskoelle/e717333c156985854559 to your computer and use it in GitHub Desktop.
Creates a submenu page for tools that lists out all custom page templates and which pages are using them. #wordpress
This file contains 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 | |
// Debug Custom Page Templates | |
function custom_page_templates_page() { | |
$templates = wp_get_theme()->get_page_templates(); | |
$done = array(); | |
$row = '<tr><td><a href="%s">%s</a></td><td width="60"><em>%s</em></td><td width="20"><a href="%s">edit</a></td></tr>'; | |
$not_found = '<tr><td colspan="3"><em>No pages found using this template.</em></td></tr>'; | |
echo '<div class="wrap"><div id="icon-tools" class="icon32"></div>'; | |
echo '<h2>Custom Page Templates</h2>'; | |
foreach ( $templates as $template_name => $template_filename ): | |
echo "<h3>$template_name ($template_filename)</h3>"; | |
echo '<table width="400">'; | |
$pages = get_pages(array( | |
'meta_key' => '_wp_page_template', | |
'meta_value' => $template_name, | |
'hierarchical' => 0, | |
'post_type' => 'page' | |
)); | |
if(!$pages) echo $not_found; | |
foreach($pages as $page){ | |
echo sprintf($row, get_permalink($page->ID), $page->post_title, $page->post_status, get_edit_post_link($page->ID)); | |
} | |
$done[] = $template_name; | |
echo '</table>'; | |
endforeach; | |
$pages = glob(TEMPLATEPATH.'/page-*'); | |
foreach($pages as $p): | |
$file = str_replace(trailingslashit(TEMPLATEPATH), '', $p); | |
if(in_array($file, $done)) continue; | |
preg_match('/page-([^.]+).php/', $file, $matches); | |
$found = get_posts(array('name' => $matches[1], 'post_type' => 'page')); | |
echo '<h3>'.$file.'</h3>'; | |
echo '<table width="400">'; | |
if($found): | |
$page = $found[0]; | |
echo sprintf($row, get_permalink($page->ID), $page->post_title, $page->post_status, get_edit_post_link($page->ID)); | |
else: | |
echo $not_found; | |
endif; | |
echo '</table>'; | |
endforeach; | |
echo '</div>'; | |
} | |
function register_tools_custom_templates_page() { | |
add_management_page( 'Template Debugging', 'Custom Page Templates', 'manage_options', 'custom-page-templates', 'custom_page_templates_page' ); | |
} | |
add_action('admin_menu', 'register_tools_custom_templates_page'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment