Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
        endif;
}

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?

share|improve this question
    
This is not the full code, is it? I mean, I cannot replicate your problem only with what you published... –  brasofilo May 28 '12 at 22:04
    
Is private a checkbox or a text field? You seem to be conflating the data types in your whitelisting code. –  Chip Bennett May 29 '12 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 May 29 '12 at 16:10
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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