Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

add meta box to Edit Comment screen WordPress

  • SOLVED

Please can someone help me add a new meta box to the 'Edit Comment' admin screen.

This meta box needs to contain one select field with three option pairs - Image 1 --> image1.png, Image 2 --> image2.png, Image 3 --> image3.png.

Values should be saved via the add_comment_meta() function.

There is no need to add a corresponding field to the frontend comment form, but values should be printable via echo get_comment_meta( $comment->comment_ID, 'comment_image', true )

Thank you

Answers (1)

2013-03-05

Arnav Joy answers:

try this

add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box() {
add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}

function extend_comment_meta_box ( $comment ) {
$comment_image = get_comment_meta( $comment->comment_ID, 'comment_image', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<label for="phone"><?php _e( 'Image' ); ?></label>
<select name="comment_image">
<option value="image1.png" <?php selected('image1.png', $comment_image);?> >Image 1 --> image1.png</option>
<option value="image2.png" <?php selected('image2.png', $comment_image);?> >Image 2 --> image2.png</option>
<option value="image3.png" <?php selected('image3.png', $comment_image);?>>Image 3 --> image3.png</option>
</select>
</p>


<?php
}

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


}


designbuildtest comments:

Fantastic. Works great thanks Arnav.