Created
September 14, 2012 22:14
-
-
Save chrisguitarguy/3725263 to your computer and use it in GitHub Desktop.
Code for a tutorial on adding columns to WordPress list tables.
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 | |
/* | |
Plugin Name: List Table Tutorial | |
Plugin URI: http://pmg.co/wp-list-table-tutorial | |
Description: How to add a column to WordPress list tables. | |
Version: 1.0 | |
Text Domain: pmg-list-table | |
Author: Christopher Davis | |
Author URI: http://christopherdavis.me | |
License: GPL2 | |
Copyright 2012 Christopher Davis | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
add_action('load-edit.php', 'pmg_ltt_load'); | |
/** | |
* Fires whenever the `edit.php` page loads in the admin area. First checks to | |
* see that we're on the pages list table, then adds the filters we need to | |
* modify the list table. | |
* | |
* @uses get_current_screen | |
* @uses add_filter | |
* @uses add_action | |
*/ | |
function pmg_ltt_load() | |
{ | |
$screen = get_current_screen(); | |
if(!isset($screen->post_type) || 'page' != $screen->post_type) | |
return; | |
add_filter( | |
"manage_{$screen->id}_columns", | |
'pmg_ltt_add_columns' | |
); | |
add_filter( | |
"manage_{$screen->id}_sortable_columns", | |
'pmg_ltt_add_sortable' | |
); | |
add_action( | |
"manage_{$screen->post_type}_posts_custom_column", | |
'pmg_ltt_column_cb', | |
10, | |
2 | |
); | |
add_filter( | |
'request', | |
'pmg_ltt_do_sort' | |
); | |
} | |
/** | |
* Hooked into `manage{$screen->id}_columns` on the page list table. Adds a | |
* new column for the page template. | |
* | |
* @param array $cols The array of columns in $key => $label pairs | |
* @return array | |
*/ | |
function pmg_ltt_add_columns($cols) | |
{ | |
$cols['template'] = __('Page Template', 'pmg-list-table'); | |
return $cols; | |
} | |
/** | |
* Hooked into `manage_{$screen->id}_sortable_columns`. Enables the template | |
* column for sorting. | |
* | |
* @param array $cols The sortable columns. $key => $url_param_value pairs | |
* @return array | |
*/ | |
function pmg_ltt_add_sortable($cols) | |
{ | |
$cols['template'] = 'template'; | |
return $cols; | |
} | |
/** | |
* Column callback. Checks to make sure we're on the `template` column, then | |
* fetchs the post template, printing out its name. | |
* | |
* @param string $col The column key | |
* @param int $post_id The post id for the row | |
* @uses get_post_meta | |
* @uses get_page_templates | |
* @uses esc_html_e | |
* @return null | |
*/ | |
function pmg_ltt_column_cb($col, $post_id) | |
{ | |
static $templates; | |
if('template' == $col) | |
{ | |
// wtf would you do $label => $filename from get_page_templates? | |
if(empty($templates)) | |
$templates = array_flip(get_page_templates()); | |
$tmp = get_post_meta($post_id, '_wp_page_template', true); | |
if($tmp && isset($templates[$tmp])) | |
{ | |
echo esc_html($templates[$tmp]); | |
} | |
else | |
{ | |
esc_html_e('Default Template', 'pmg-list-table'); | |
} | |
} | |
} | |
/** | |
* Hooked into `request` modifies the query to include custom sorting if needed | |
* | |
* @param array $vars The query variable | |
* @param array | |
*/ | |
function pmg_ltt_do_sort($vars) | |
{ | |
if(isset($vars['orderby']) && 'template' == $vars['orderby']) | |
{ | |
// WordPress is trying to order by template. Let's modify the query to | |
// order by the appropriate meta key. | |
$vars['meta_key'] = '_wp_page_template'; | |
$vars['orderby'] = 'meta_value'; | |
} | |
return $vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment