Adding custom columns to the WordPress manage posts 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
An under-documented technique available to WordPress plugin developers that I use in Zensor is adding custom columns to the Manage Posts screen in the admin interface. A search on Google or the Codex turns up very little. The only references on the Codex are on the pages listing all Filters and Actions.
The action/filter that I’m talking about is the complementary pair manage_posts_columns and manage_posts_custom_column. Combined, they let a plugin developer add new columns or shift around/delete existing columns on the Manage Posts screen.
The Manage Posts screen is accessed from the ‘Manage’ menu option in the administration interface. Clicking on that will give you a screen somewhat like the one to the right. Your posts are listed with options to filter/edit/delete/etc them. This columns you see here are the ones that we will be changing with this technique. In fact, you can change almost all of the columns (just not the View/Edit/Delete ones).
Requirements
The following sections will assume you know how to create a plugin (see Writing A Plugin).
Removing a column
Consider for a second that you’re the only user on your blog and you don’t allow comments. There’s no real need to see for every post that you wrote it and that nobody has commented on it. Let’s try to get rid of those columns.
For this task, we need to use the manage_posts_columns filter to remove the Comments and Author columns. manage_posts_columns is a filter that is passed an associative array that associates “internal name” to “display name” for each individual column. This happens at around line 137 in wordpress/wp-admin/edit.php (in WordPress version 2.2). We have to look in the code to find the “internal name” of the Comments and Author columns, so we know what to remove. A quick check reveals that the names are comments and author, surprisingly. With these values, we can write our plugin code:
add_filter('manage_posts_columns', 'scompt_custom_columns');
function scompt_custom_columns($defaults) {
unset($defaults['comments']);
unset($defaults['author']);
return $defaults;
}
Notice that all we do is get rid of the associations for comments and author. The results can be seen to the right.
Adding a column
A more practical example is to add a new column to the mix. We’re going to add a column that lists any post attachments. To do this, we first need to add our column to the list of columns, similar to what we did in the previous step:
add_filter('manage_posts_columns', 'scompt_columns');
function scompt_columns($defaults) {
$defaults['attachments'] = __('Attachments');
return $defaults;
}
attachments is the internal name that we will use in the next step to add data to the column. Attachments is the text that will be displayed in the column header. It’s localized to spread to the love. If you look at your Manage Posts screen now, you’ll see an empty Attachments column, as can be seen to the right.
To add the data to our attachments column, we need to hook onto the manage_posts_custom_column action. This action is called for each post whenever an unknown column is encountered. It receives two parameters: the internal name of the column and the ID of the current post. We’ll hook onto the action using the following code:
add_action('manage_posts_custom_column', 'scompt_custom_column', 10, 2);
Here’s the skeleton of the scompt_custom_column method dumping the parameters that it received:
function scompt_custom_column($column_name, $id) {
if( $column_name == 'attachments' ) {
var_dump( $column_name );
var_dump( $id );
}
}
Notice that the first thing being done is to check that the column name is the same as the internal name that we created in scompt_columns. This lets a single function handle multiple custom columns. You should always add this check, even if you’re only adding a single column as the user may have other plugins that are also adding custom columns. A second thing to notice is that we’re outputting the content directly (using var_dump), not returning it.
Now we can move onto the actual implementation of the scompt_columns function. Here it is:
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 important bits to notice are the part where we check the $column_name variable to make sure we’re in the right column and the lookup of any attachments using the post ID that was passed into the function. The rest of the function just pretties up the output and displays it. The results of the completed function can be seen to the right.
Miscellaneous
Inside your manage_posts_custom_column, you also have access to the global $post variable that holds a reference to the current post being displayed. This is further explained at ifacethoughts, but in short it means access to all sorts of goodness.
What about pages?
You might have noticed that up until this point we’ve only talked about custom columns on the Manage Posts screen. There’s a reason for that… you can’t change the columns on the Manage Pages screen. It’s always been like this and for the past couple years, there’s been an open ticket asking for it to be resolved. This hasn’t happened yet and who knows when it will happen. Keep an eye on scompt.com for a possible solution though.
Conclusion
This short tutorial has showed two the two most common uses for the manage_posts_columns filter and the manage_posts_custom_column action: deleting columns and adding columns to the Manage Posts screen. This is a valuable technique for giving your contributors a bit more context when looking at their posts. Now you’ll be able to implement it in your next plugin.
Abhijit Nadgouda Said,
October 20, 2007 @ 10:30 pm
A nice tutorial. The admin hooks are not very popular, but they can be important for customizations sometimes.
Weblog Tools Collection » Blog Archive » WP Plugin: Custom Hooks for Developers Said,
October 24, 2007 @ 6:40 am
[...] author has also written a tutorial on how to add custom columns to the manage posts screen which also applies to these hooks. (No Ratings Yet) Loading … Sphere: Related [...]
IhateDesign Said,
October 24, 2007 @ 12:38 pm
thanks for the tut!!!
enthusiasm : archive : » links for 2007-10-24 Said,
October 24, 2007 @ 6:39 pm
[...] scompt.com » Adding custom columns to the WordPress manage posts screen Tutorial for adding columns to post management page in WordPress admin (tags: wordpress, dev, plugin) [...]
josh b Said,
December 5, 2007 @ 1:34 pm
thank you so much for posting this. I saw the filter and action but no documentation.
jeremy clarke Said,
January 18, 2008 @ 1:46 pm
Thanks for writing this man. Great article and very useful for all kinds of things. This should be in the codex somewhere… I don’t know why it doesn’t have a system for adding a detail page for each hook and how to use it, the descriptions there are almost always not useful…
Just added it as a link to this page after the short descriptions on the filter and action listing pages.
Thanks!
Hugh Todd Said,
February 18, 2008 @ 5:42 am
This looks wonderful, and I’ve got started by using Hello Dolly. But I have hit a wall and need help.
The new column is created, but I can’t for the life of me work out how to add the particular data I’d like to put in the new column. And here’s where my almost total lack of coding knowledge is exposed.
What I’d like to add to the column is the data from each Post’s single Custom Field. What code would I need to use to achieve this? (The Key for the custom field is “Ref”, and the Value is an alphanumeric string.)
Any help with this would be appreciated enormously.
scompt Said,
February 18, 2008 @ 6:05 am
Hi Hugh,
You’ll probably need something like this:
Hugh Todd Said,
February 18, 2008 @ 5:20 pm
Edward, that’s perfect. Many thanks. It’s up and running.
Now (there’s always more, isn’t there?) is it possible to add a filter for this custom column, I wonder?
scompt Said,
February 19, 2008 @ 2:53 am
I don’t understand what you mean. What exactly are you looking to do?
Hugh Todd Said,
February 19, 2008 @ 6:08 pm
On the Manage > Posts page, in WP 2.3, there’s a search function, allowing filtering by string, status, author, month and category. Are there any hooks to allow further filters? By ID, for example, or by whatever has been added to a custom column (in this case reference numbers)?
scompt Said,
February 20, 2008 @ 8:49 am
There are indeed some hooks to allow further filters, but their use is a bit more complicated. Maybe I’ll write up a blog post about them sometime soon. The big one you’ll want to look at is
.
Hugh Todd Said,
February 21, 2008 @ 3:36 am
Ah, excellent. I’ll keep an eye out for future pearls. Very, very helpfut, thanks.
Post and Page IDs In WordPress Admin - Peter Coughlin Said,
August 6, 2008 @ 10:14 am
[...] the way, I wouldn’t have done it without the info provided by Edward Dale. Thanks buddy. del.icio.us Digg Propeller [...]
WordPress Action Hooks · Yakup Gövler'in Not Defteri Said,
September 18, 2008 @ 8:44 am
[...] filtresini inceleyin. Bu ekrana yeni sütun eklemede kullanılır. Örnek için burayı inceleyin. manage_pages_custom_column Yukarıdakinin sayfa için olanı. password_reset [...]