Add Custom Fields to Comments

I already have plenty of tutorials about working with custom fields – a tutorial about creating custom meta boxes for posts, a tutorial about custom taxonomy term fields, a tutorial about options pages and even my own Simple Fields plugin intended to simplify the work with all that stuff. Now it is time to talk about meta boxes for comments.

The long story short let me show you what we are going to create:

create a custom metabox for WordPress comments

It is not a very difficult meta box, but I am just going to show you the process how it works, everything else is up to you. We are going to do it without any plugins first.

Add a Meta Box for Comments

The process of adding a meta box for edit comments pages is so similar to regular posts meta boxes. The codes may go to your current theme functions.php file or a custom plugin.

add_action( 'add_meta_boxes_comment', 'rudr_comment_meta_box' );
function rudr_comment_meta_box( $comment ) { // WP_Comment object

	add_meta_box( 
		'rudr_comment', 
		'Comment Settings', 
		'rudr_comment_meta_box_cb', 
		'comment', // instead of a post type parameter
		'normal'
	);
	
}

function rudr_comment_meta_box_cb() {
	echo 'Hi!';
}

In order to add a meta box, I used add_meta_box() function on add_meta_boxes_comment action hook. But you can also use add_meta_boxes filter hook by the way.

add_action( 'add_meta_boxes', 'rudr_comment_meta_box', 25, 2 );
function rudr_comment_meta_box( $type, $comment ) {

	if( 'comment' !== $type ) {
		return;
	}
Comment meta box without fields

Add Fields to Comment Settings

Now it is time to do some work with meta box callback function.

<?php
function rudr_comment_meta_box_cb( $comment ) {

	$comment_rating = get_comment_meta( $comment->comment_ID, 'comment_rating', true );

	wp_nonce_field( 'rudr_comment_update', 'comment_nonce' );
	?>
		<table class="form-table">
			<tr>
				<th><label for="comment_rating">Rating</label></th>
				<td>
					<select id="comment_rating" name="comment_rating">
						<option value="">Please choose…</option>
						<?php
							for( $i = 1; $i <=5; $i++ ) {
								echo "<option value=\"$i\"" . selected( $i, $comment_rating, true ) . ">$i</option>";
							}
						?>
					</select>
				</td>
			</tr>
		</table>
	<?php
}

I didn’t use any escaping functions here, because we can trust WordPress selected() function and that’s actually the only place we are using the data we get from the database.

Save Comment Metadata

add_action( 'edit_comment', 'rudr_save_comment' );
function rudr_save_comment( $comment_id ) {

	if( ! isset( $_POST[ 'comment_nonce' ] ) || ! wp_verify_nonce( $_POST[ 'comment_nonce' ], 'rudr_comment_update' ) ) {
		return;
	}

	update_comment_meta( 
		$comment_id, 
		'comment_rating', 
		absint( $_POST[ 'comment_rating' ] ) 
	);

}

As long as we have only 1 to 5 values in our custom fields, we can easily use absint() function for sanitization.

Now you can get commend field value anywhere on your website using get_comment_meta() function.

Yet Another Example

In this example we are going to create the same metabox using my Simple Fields plugin.

  1. Install and activate the plugin on your website.
  2. Copy and paste the following code into your current theme functions.php file or to a custom plugin.
add_filter( 'simple_register_comment_settings', 'misha_comments_metabox' );

function misha_comments_metabox( $metaboxes ) {

	$metaboxes[] = array(
  		'id'	=> 'rudr_comment',
  		'name'	=> 'Comment settings',
  		'fields' => array(
 			array(
 				'id' => 'comment_rating',
 				'label' => 'Rating',
 				'type' => 'select',
 				'options' => array(
					''  => 'Please choose…'
 					'1' => '1',
 					'2' => '2',
 					'3' => '3',
 					'4' => '4',
					'5' => '5'
 				),
 			)
  		)
  	);

 	return $metaboxes;

}

And we have the same result:

create a custom metabox for WordPress comments
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X