Adding custom columns to the WordPress manage pages screen
- Adding custom columns to the WordPress manage posts screen
- Manage Pages Custom Columns plugin released
- Adding custom columns to the WordPress manage pages screen
- Custom filtering in the WordPress Manage Posts screen
- Updating the Manage Posts Filtering for WordPress 2.5
I wanted to tie together my two recent posts on adding custom columns to the WordPress manage posts screen and the Manage Pages custom columns plugin I released. Below, I’ll extend the code from the first post to use the plugin announced in the second post to add a custom column or two to the Manage Pages screen.
Requirements
This tutorial assumes that you’ve already implemented the code from my previous post. Also, you must already have the Manage Pages Custom Columns plugin installed.
Existing Code
The code we begin with comes directly from the previous post. It’s reproduced below:
add_filter('manage_posts_columns', 'scompt_columns');
add_action('manage_posts_custom_column', 'scompt_custom_column', 10, 2);
function scompt_columns($defaults) {
$defaults['attachments'] = __(’Attachments’);
return $defaults;
}
function scompt_custom_column($column_name, $post_id) {
global $wpdb;
if( $column_name == ‘attachments’ ) {
$query = “SELECT post_title, ID FROM $wpdb->posts “.
“WHERE post_type=’attachment’ “.
“AND post_parent=’$post_id’”;
$attachments = $wpdb->get_results($query);
if( $attachments ) {
$my_func = create_function(’$att’,
‘return “<a href=\”".get_permalink($att->ID).”\”>”.
$att->post_title.
“</a>”;’);
$text = array_map($my_func, $attachments);
echo implode(’, ‘,$text);
} else {
echo ‘<i>’.__(’None’).’</i>’;
}
}
}
The gist of the code is that we add a custom “Attachments” column to the Manage Posts screen in the scompt_columns function. This column is then populated from the scompt_custom_column function.
Changes for the Manage Pages screen
Converting this code to work on the Manage Pages screen is extremely simple. This will be true in the majority of cases. To do the conversion, we just need to use the new hooks provided by the Manage Pages Custom Columns plugin: manage_pages_custom_column and manage_pages_columns. The following code attaches the functions that we defined above to these hooks:
add_filter('manage_pages_columns', 'scompt_columns');
add_action('manage_pages_custom_column', 'scompt_custom_column', 10, 2);
Notice that the actual scompt_* functions don’t need be changed at all. With this code implemented, the Manage Pages screen should look similar to the one at the right.