Add ID and Parent Comment ID Columns to the Comments Table in /wp-admin

My task was to add two columns to the wp-admin/edit-comments.php page – with comment ID itself and its parent comment ID. Something like this:

Comment ID and Parent Comment ID columns. I will also show you how to customize their width.
Actually you can add these columns anywhere you want – maybe before the Author column or maybe at the end.
add_filter( 'manage_edit-comments_columns', 'rudr_add_comments_columns' );
 
function rudr_add_comments_columns( $my_cols ){
	// $my_cols is the array of all column IDs and labels
	// if you know arrays, you can add, remove or change column order with no problems
	// like this:
	/*
	$my_cols = array(
		'cb' => '', // do not forget about the CheckBox
		'author' => 'Author',
		'comment' => 'Comment',
		'm_comment_id' => 'ID', // added 
		'm_parent_id' => 'Parent ID', // added
		'response' => 'In reply to',
		'date' => 'Date'
	);
	*/
	// but the above way is not so good - there could be problems when plugins would like to hook the comment columns
	// so, better like this:
	$misha_columns = array(
		'm_comment_id' => 'ID',
		'm_parent_id' => 'Parent ID'
	);
	$my_cols = array_slice( $my_cols, 0, 3, true ) + $misha_columns + array_slice( $my_cols, 3, NULL, true );
 
	// if you want to remove a column, you can just use:
	// unset( $my_cols['response'] );
 
	// return the result
	return $my_cols;
}
 
 
 
add_action( 'manage_comments_custom_column', 'rudr_add_comment_columns_content', 10, 2 );
 
function rudr_add_comment_columns_content( $column, $comment_ID ) {
	global $comment;
	switch ( $column ) :
		case 'm_comment_id' : {
			echo $comment_ID; // or echo $comment->comment_ID;
			break;
		}
		case 'm_parent_id' : {
			// try to print_r( $comment ); to see more comment information
			echo $comment->comment_parent; // this will be printed inside the column
			break;
		}
	endswitch;
}

Let me suppose that your newly created comment columns are too wide, so this small piece of code should help:

add_action( 'admin_head', 'rudr_columns_too_wide' );
 
function rudr_columns_too_wide(){
	echo '<style>#m_comment_id, #m_parent_id {width: 100px;}</style>';
}

P.S. Yes, everything should be added to functions.php.

Related Posts

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 11 yrs of experience.

Need some custom developer help? Get in touch