Adding custom columns to the WordPress comments admin page

Adding custom columns to the WordPress admin pages is fairly easy, however not all the filters available to you are displayed on Plugin API Filter Reference page. The following article will take you through the process of adding additional columns to the comments admin page using two undocumented filters.

If you are not a plugin author intending to use this functionality within a plugin, you should place the following code examples into your theme’s functions.php file.

The first filter “manage_edit-comments_columns” is used to adjust the table headings by appending a column called “My Custom Column” to the end of the table headings.

function myplugin_comment_columns( $columns )
{
	$columns['my_custom_column'] = __( 'My Custom Column' );
	return $columns;
}
add_filter( 'manage_edit-comments_columns', 'myplugin_comment_columns' );

The second filter “manage_comments_custom_column” will populate each comment row with data. It checks that the column ID matches that set in the function above (“my_custom_column”) and outputs “Custom Data for ID:” followed by the comment ID  for each row.

function myplugin_comment_column( $column, $comment_ID )
{
	if ( 'my_custom_column' == $column ) {
		echo 'Custom Data for ID: ' . $comment_ID;
	}
}
add_filter( 'manage_comments_custom_column', 'myplugin_comment_column', 10, 2 );

Outputting this text isn’t really that useful to anyone, instead you will more likely want to show some actual data related to the comment. One thing you may want to do is adjust the function to display some meta data stored for the comment.

function myplugin_comment_column( $column, $comment_ID )
{
	if ( 'my_custom_column' == $column ) {
		if ( $meta = get_comment_meta( $comment_ID, $column , true ) ) {
			echo $meta;
		}
	}
}

The above snippet assumes that meta key is the same as the column ID (“my_custom_column”) and you only want to add a single column, but what if you want to add multiple columns and show different data?

function myplugin_comment_columns( $columns )
{
	return array_merge( $columns, array(
		'custom_column_one' => __( 'Custom Column One' ),
		'custom_column_two' => __( 'Custom Column Two' ),
		'custom_column_three' => __( 'Custom Column Three' )
	) );
}
add_filter( 'manage_edit-comments_columns', 'myplugin_comment_columns' );

function myplugin_comment_column( $column, $comment_ID )
{
	switch ( $column ) {
		case 'custom_column_one':
		case 'custom_column_two':
			if ( $meta = get_comment_meta( $comment_ID, $column , true ) ) {
				echo $meta;
			} else {
				echo '-';
			}
		break;
		case 'custom_column_three':
			echo 'Third Column Data';
		break;
	}
}
add_filter( 'manage_comments_custom_column', 'myplugin_comment_column', 10, 2 );

The above will add three columns to the edit page, the first two will try to lookup comment meta data and the third output the text “Third Column Data” for the sake of this example.

Similar function and filter combinations can be used for adding columns to the other admin pages. Just adjust the filters used by replacing {type} with either ‘posts‘ or ‘pages‘ in the filters below, however we will not go into further detail here.

  • manage_{type}_columns
  • manage_{type}_custom_column

Please note that the above functions have only been tested using WordPress 3.2.1 and cannot guarantee they will work correctly or the same in older versions.

7 thoughts on “Adding custom columns to the WordPress comments admin page”

  1. I was looking for a way to add extra meta field to my comment area to get additional data from users, but I can see how I could apply your codes to get it.

  2. This tutorial would be so much better if you could edit the data in the column and save it back to the database.

  3. Great stuff here, thanks for sharing it!

    @Toure, @Alex
    I need to say that you are fucking pathetic! An article title reads “Adding custom columns to the WordPress comments admin page” and that’s exactly what it’s all about. This guy has put some efforts to write it clear an loud and if you don’t appreciate it – shut a fuck up.

    1. what an ignorant bastard you are!
      I know what the title says, but is it wrong to use the code as inspiration for a different idea? That exactly what smart people do. I simply modified the author code for something else and I informed him about it. Read first before opening that mouth.
      “I can see how I could apply your codes to get it”

  4. Great article! Really helped! Thanks!
    Could you give an advise on how to add some data to core-columns on the same screen?
    Thanks in advance!

  5. Great post indeed. I was just wondering whether “function myplugin_comment_column” should be hooked with “add_filter” or “add_action”?!
    I understand that we are talking about comment column instead of post/page/cpt column here; but I couldn’t find WordPress Codex document on custom comment column.

    Please correct me if I am wrong. Thank you.

    Source : WordPress Codex

Leave a Reply