0

I have been trying for two weeks to add a edit custom meta field to the admin edit comment form. I managed to show the field in the form, but it can't get it to update.

I would also like to add this field to the registration form and profile.

this is what I have so far

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
    add_comment_meta( $comment_id, 'location', $_POST['location'] );
}

add_action( 'add_meta_boxes', 'location_meta' );
function location_meta() {
    add_meta_box( 'location_meta_id', 'Location of the Comment Author', 'location_meta_call', 'comment', 'normal', 'high' );
}

function location_meta_call(){
    $location = get_comment_meta(get_comment_ID(),'location', true);
    ?>
    <label for="location">Location:</label>
    <input type="text" name="location_meta_box_text"  value="<?php echo $location ?>" id="location_meta_box_text" />
    <?php
}

add_filter('comment_save_pre','location_save');
function location_save($comment_content){
    global $wpdb;

    $id = get_comment_ID();
    $text = $_POST['location_meta_box_text'];
    $data = compact('text');
    $rval= $wpdb ->update($wpdb->comments,$data,compact('id'));

    update_comment_meta($id,'location',$text);

    return $comment_content;

}
1

The get_comment_ID function was not working.
So use following code in location_meta_call function

function location_meta_call(){
    $location = get_comment_meta($_GET["c"],'location', true);
}

add_filter('comment_save_pre','location_save');

function location_save($comment_content){
    global $wpdb;

    $id = $_POST['comment_ID'];
    $text = $_POST['location_meta_box_text'];

    update_comment_meta($id , 'location', $text);
    return $comment_content;
}
  • finally got around to working on this and it finally worked. Thanks – alme1304 Oct 7 '12 at 2:20
0

Try this:

function location_save($comment_content){
    $id = $_POST['comment_ID'];
    $text = $_POST['location_meta_box_text'];
    update_comment_meta($id,'location',$text);
    return $comment_content;
}

The get_comment_ID function wasn't working.

I'm not sure where you were getting the $rval/compact code from (I saw it in the core), but the compact needs to be compact('comment_ID') and the $id var needs to be $comment_ID for that to work. (I think...) Anyway, I wouldn't save it this way! Just use the update comment meta function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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