WordPress wp-admin shows you a list of posts and pages with their details such as title, author, categories, tags, comments, date.
Sometimes you might want to add new customized column to this page, such as ‘Author Gravatar’, ‘Views’ or ‘Thumbnails’. This post will demonstrate how to go about it.
Here we are going to add post author gravatar column to post management page without the need of any third-party plugin.
We can add this column using theme or plugin. From the below image you will get overview what I am going to explain you.
First we need to register the column which we are going to add. Register column using "manage_posts_columns" filter.
add_filter( 'manage_posts_columns', 'rt_add_gravatar_col'); function rt_add_gravatar_col($cols) { $cols['author_gravatar'] = __('Gravatar'); return $cols; }
The function ‘rt_add_gravatar_col’ takes one argument ‘$cols’. Which is an array of the default registered columns.
Now we have to add action ‘manage_pages_custom_column’ to display new column. You can add action hook as follows:
add_action( 'manage_posts_custom_column', 'rt_get_author_gravatar'); function rt_get_author_gravatar($column_name ) { if ( $column_name == 'author_gravatar' ) { echo get_avatar( get_the_author_email(), '35' ); } }
The function ‘rt_get_author_gravatar’ takes one argument ‘$column_name’ which is the name of the column. WordPress get_avatar( get_the_author_email(), ’35’ ) gives you gravatar of an author. As I used the ‘get_avatar’ function code you can use any other function code.
You can use the filter '
manage_pages_columns‘ for page management and action ‘manage_pages_custom_column‘ to display the column.
Similarly for custom post types you can use ‘manage_movie_columns’ filter and ‘manage_movie_custom_column’ action. Here I am used movie as custom post type. Replace ‘movie’ word with your custom post type.
Hopefully this will help you add a custom column to the post and page overview. Do drop in your views and suggestions through your comments.
10 Comments
Nice tutorial on posts management panel.
Thanks Pradeep
Hey Umesh, good tut . . .I’ll defiantly use it in future. Thanks for sharing.
Hey, nice find Umesh!
Also, we can remove certain columns from there using same filter!
e.g.
unset($posts_columns[‘comments’]);
Rather whole template.php file (where manage_posts_columns filter is “applied”) has many more hooks to attach a function for changing/modifying default WordPress templates behavior.
That shows how flexible WordPress Core is!
Thanks a lot for sharing 🙂
I’m sorry, but in whitch file I have to make these changes?
If you want to add columns on theme activation add this code in themes functions.php or if you want to add columns on plugin activation add this code in your plugin file.
@Daniele – you can add this code to your theme’s functions.php file
the problem is that there isn’t a filter for managing comments column.
There is a action hook in the name of “manage_comments_custom_column”
but there isn’t a filter hook to go with, like “manage_comments_columns”
I’ve looked everywhere for that filter hook… and nothing.
What can be the solution?
Cheers,
Asaf.
Use this filter hook instead:
manage_edit-comments_columns
It’s the old style, but works just fine for WP 3.4.2