rwr1725 rwr1725 - 1 year ago 47
PHP Question

Can't edit comment meta data in Wordpress

I am using Wordpress (version 4.5.3) and have added a custom field to the comment form. The field is a drop down list using the select tag. The field shows up in the form just fine, and I can choose an option and the value for that option gets saved properly.

I have also added code to edit the custom field in the Edit Comment page (/wordpress/wp-admin/comment.php?action=editcomment). I am able to retrieve the stored value and then recreate the drop down list with the proper value selected.

I have added code to save the edited value for the custom field but that is not working. When, from the Edit Comment page, I select a different value from the drop down and then click on the "update" button the newly selected value does not get saved.

I am adding the code to do all this in functions.php. Here's the code to add the field to the form and store the data:

// Add fields after default fields above the comment box, always visible

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );

function additional_fields () {

echo '<p class="comment-form-area">'.
'<label for="region">' . __( 'Choose a <strong>region</strong>' ) . '<span class="required">*&nbsp;&nbsp;&nbsp;&nbsp;</span></label>'.
'<br /><select id="region" name="region">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>

// Save the comment meta data along with comment

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {

if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') )
$region = wp_filter_nohtml_kses($_POST['region']);
add_comment_meta( $comment_id, 'region', $region );


Here is the code to add the custom field to the comment editing page:

// Add an edit option to comment editing screen

add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );

function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Region field' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );

function extend_comment_meta_box ( $comment ) {

$_regions = array

$region = get_comment_meta( $comment->comment_ID, 'region', true );
<label for="region"><?php _e( 'Region*' ); ?></label>
<p><select id="region" name="region">
for ($ix = 0; $ix < count($_regions); $ix++) {
echo '<option value="' . $_regions[$ix] . '"';
if ($region == $_regions[$ix]) {
echo ' selected';
echo '>' . $_regions[$ix] . '</option>';


All this seems to be working fine. Here is the code to save the edited value for the custom field:

// Update comment meta data from comment editing screen

add_action( 'edit_comment', 'extend_comment_edit_metafields' );

function extend_comment_edit_metafields( $comment_id ) {
if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;

if ( ( isset( $_POST['region'] ) ) && ( $_POST['region'] != '') ) :
$region = wp_filter_nohtml_kses($_POST['region']);
update_comment_meta( $comment_id, 'region', $region );
else :
delete_comment_meta( $comment_id, 'region');


This code does not work. When I return to the Edit Comment page I see the original value for the custom field, not the edited value.

Why isn't the edited value being saved?

Answer Source

The function extend_comment_edit_metafields() initially tests to see if the nonce field has been set. It has not so the return executes without calling update_comment_meta(). To work properly the nonce field should be set in extend_comment_meta_box() like this:

wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );

Discussion of the nonce field is here: