How to Add Columns to a Comments List Table in WordPress

How to Add Columns to a Comments List Table in WordPress

Hello. In today’s article, we’ll cover the topic of comments. Specifically, how to add a custom column to the comment list table and display custom (non-standard) values in it.

The logic of adding a new column to the comment table is similar to adding extra columns in the post list table (both default and custom post types). However, in the case of comments, we need to use the following hooks:

manage_edit-comments_columns — to add the column header;
manage_comments_custom_column — to fill the table cell with content;

Since I’m using a clean version of WordPress installed on a local server for this article, I’ll have to create comments manually or use the WP CLI command line tool. If you’re not familiar with it yet, I strongly recommend starting to use it. WP-CLI saves a huge amount of time, which will definitely add up over time.

For example, let’s generate 13 comments for the post with ID 13:

wp comment generate --count=13 --post_id=13

Simple, and most importantly — fast!

Let’s get back to the code. Open your active theme’s functions.php file and insert the following code:

add_filter('manage_edit-comments_columns', function($columns)
{
	$columns['custom_col_title'] = 'Custom column';
		
	return $columns;
}, 10);

The hook function takes an array of existing column names as input. We simply add a new column name to this array under the index “custom_col_title”.

Next, go to the Comments page in the admin dashboard. You should see a new column titled “Custom column” but without any content.

To fill in the column cells, add the following PHP code just below:

add_action('manage_comments_custom_column', function($column, $comment_ID)
{
	if($column == 'custom_col_title')
	{
		echo get_comment_meta($comment_ID, 'custom_col_data', true);
	}
}, 10, 2);

This hook function receives two parameters:
$column — the column key. It is used to check which column we’re displaying the value for. You can also override values in other columns in the same way.
$comment_ID — the ID of the current comment. In our case, we’ll use it to fetch data from the comment meta table.

As mentioned above, the condition:

if($column == 'custom_col_title')
{
	echo get_comment_meta($comment_ID, 'custom_col_data', true);
}

is used to check which value should be displayed in which column.

If your custom column is still empty after all these steps, don’t worry. It’s possible you just haven’t added a value in the database for the corresponding comment using the key “custom_col_data”. You can use the following SQL query (replace the comment ID with the one from your DB):

INSERT INTO `wp_commentmeta` (`meta_id`, `comment_id`, `meta_key`, `meta_value`) VALUES (NULL, '10', 'custom_col_data', 'Some value');
Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

Your email address will not be published. Required fields are marked *