I need help with why the save function is not working in the edit_comment hook.

Here is the code for the 'comment_post' hook:

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
    if ( ( isset( $_POST['private'] ) ) && ( $_POST['private'] != '') )
    $private = wp_filter_nohtml_kses($_POST['private']);
    add_comment_meta( $comment_id, 'private', $private );

And here is the 'edit_comment' hook:

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['private'] ) ) && ( $_POST['private'] != '') ) :
        $private = wp_filter_nohtml_kses($_POST['private']);
        update_comment_meta( $comment_id, 'private', $private );
        else :
        delete_comment_meta( $comment_id, 'private', $private );

This adds a checkmark box in the edit window to make the comment private.

The checkmark box appears but it does not save when I update the comment in the edit window. Any idea why?

  • This is not the full code, is it? I mean, I cannot replicate your problem only with what you published...
    – brasofilo
    Commented May 28, 2012 at 22:04
  • Is private a checkbox or a text field? You seem to be conflating the data types in your whitelisting code. Commented May 29, 2012 at 16:07
  • @ChipBennett - I'm basing this off a tutorial so you may be right. I don't think I need this: wp_filter_nohtml_kses. Also, I don't think I need wp_nonce_field (verified by $_POST['extend_comment_update']. I think these two verification techniques are used for string inputs and can be left out for checkboxes.
    – AlxVallejo
    Commented May 29, 2012 at 16:10

1 Answer 1


I think you've way over-complicated things. Assuming private is a checkbox, just check for it being set, and act accordingly. With form $_POST data, a checkbox will send its value if checked, and will send nothing if not checked:

if ( isset( $_POST['private'] ) ) {
    update_comment_meta( $comment_id, 'private', 'private' );
} else {
    delete_comment_meta( $comment_id, 'private', 'private' );

Note that, since a checkbox is binary (checked/unchecked), you can hard-code the value into your update_comment_meta() call, thereby inherently sanitizing the user input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.