Created
December 11, 2012 03:04
-
-
Save ramseyp/4255610 to your computer and use it in GitHub Desktop.
Add a page template column to WordPress's Pages view.
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 | |
/** | |
* | |
* Add page template column to page listing. Make the Template column sortable | |
* http://www.codehooligans.com/2011/07/13/adding-custom-columns-to-wordpress-post-listing/ | |
* http://justintadlock.com/archives/2011/06/27/custom-columns-for-custom-post-types/ | |
* | |
*/ | |
add_filter( 'manage_edit-page_columns', 'admin_page_header_columns', 10, 1); | |
add_action( 'manage_pages_custom_column', 'admin_page_data_row', 10, 2); | |
add_filter( 'manage_edit-page_sortable_columns', 'my_page_sortable_columns' ); | |
add_action( 'load-edit.php', 'my_edit_page_load' ); | |
function admin_page_header_columns($columns) { | |
if (!isset($columns['template'])) | |
$columns['template'] = "Template"; | |
return $columns; | |
} | |
function admin_page_data_row($column_name, $post_id) { | |
switch($column_name) { | |
case 'template': | |
global $post; | |
$template_file = get_post_meta( $post_id, '_wp_page_template', TRUE ); | |
echo $template_file; | |
break; | |
default: | |
break; | |
} | |
} | |
function my_page_sortable_columns( $columns ) { | |
$columns['template'] = 'template'; | |
return $columns; | |
} | |
function my_edit_page_load() { | |
add_filter( 'request', 'my_sort_pages' ); | |
} | |
function my_sort_pages( $vars ) { | |
if ( isset( $vars['post_type'] ) && 'page' == $vars['post_type'] ) { | |
if ( isset( $vars['orderby'] ) && 'template' == $vars['orderby'] ) { | |
$vars = array_merge( | |
$vars, | |
array( | |
'meta_key' => '_wp_page_template', | |
'orderby' => 'meta_value' | |
) | |
); | |
} | |
} | |
return $vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment