2

All guest comments are user_id = 0, while registered users have their unique user_id attach to the comment. How to edit comment user_id from the comment edit screen.

I think I need to use edit_comment and add_meta_boxes_comment hooks.

3
+25

You can take reference from below code. It will provide an option to update user id of comment (https://prnt.sc/s57q4f). paste below code in active theme's functions.php file.I have tested and it is working for me. let me know if it works for you.

# comment user id update metabox
function action_add_meta_boxes_comment( $comment ) {
    ?>
    <div id="commnet-user-id" class="stuffbox">
        <div class="inside">
        <h2 class="edit-comment-userid"><?php _e( 'Commment User ID' ); ?></h2>
        <fieldset>
        <legend class="screen-reader-text"><?php _e( 'Comment User ID' ); ?></legend>
        <table class="form-table editcomment" role="presentation">
        <tbody>
        <tr>
            <td class="first"><label for="name"><?php _e( 'User ID' ); ?></label></td>
            <td><input type="text" name="newcomment_userid" size="30" value="<?php echo esc_attr( $comment->user_id ); ?>" id="user_id" /></td>
        </tr>

        </tbody>
        </table>
        </fieldset>
        </div>
        </div>
    <?php
};          
// add the action 
add_action( 'add_meta_boxes_comment', 'action_add_meta_boxes_comment', 10, 1 ); 

add_filter( 'comment_edit_redirect',  'save_edit_comment_data', 10, 2 );
function save_edit_comment_data( $location, $comment_id )
{
    $_POST['user_id'] = (int) $_POST['newcomment_userid'];
    wp_update_comment( $_POST );
    return $location;
}
| improve this answer | |
2

As an alternative option, you may use wp_update_comment_data filter which is right before default update to database.

Pros

  • don't need to do any saving mechanism and focus of data preparation
  • have full control of all data before saving

This example used add_meta_boxes while @Chetan Vaghela is using add_meta_boxes_comment. Their calling location in edit-form-comment.php is just side by side, so result should be the same. You may also combine both method by using @Chetan Vaghela meta box with the comment data filter wp_update_comment_data to further simplify the solution

If using add_meta_box function, could only use position normal. It will not be rendered in other locations.

add_action( 'add_meta_boxes', 'ws364679_register_meta_boxes', 10, 2 );
add_filter( 'wp_update_comment_data', 'ws364679_save_comment' );
function ws364679_register_meta_boxes( $post_type, $comment )
{
    // this example use an add_meta_box + a metabox callback or just use your own html here and use the $comment variable as reference
    add_meta_box('user_id_box', 'User ID:', 'ws364679_metabox_callback', 'comment', 'normal', 'default', $comment );
}

function ws364679_metabox_callback( $comment )
    {
// if using id ane name 'user_id' rather than comment_user_id, you don't need even need to use 'wp_update_comment_data' filter because by default 'user_id' will be prepared as the variable for putting into database. You may optionally use the filter for final checking.
    ?>
        <div class="custom-meta-box-container">
            <div class="form-row">
                <input type="text" id="comment_user_id" name="comment_user_id" value="<?php echo $comment->user_id; ?>"/>

            </div>
        </div>
    <?php
    }

function ws364679_save_comment( $data ) {
    // because user id is prepared somewhere before it is saved, so it is suitable to add any changes to `wp_update_comment_data` filter
    // in this filter $_REQUEST has been processed and prepared in $data

    // because username and email could be manually updated in the ui, you may add an automatic logic here to match your new user id by fetching it from data base

    // if the input box id and name is 'user_id', no need to do this manipulation
    $data['user_id'] = $data['comment_user_id'];

    // any checking if you need before return

    return $data;
}

I tested the above code in a default theme and proved to work. While it update user id, you may also consider update the user information automatically by fetching user information by the id during the data manipulation.

| improve this answer | |
1

Thanks, everyone for the answers. I already solved it. This is my code. I was trying edit_comment at the time of questioning.

function wp_review_id_metabox_full() {

    if (get_comment_type($comment->comment_ID) == 'wp_review_comment') {

        add_meta_box('some_meta_box_name',  __( 'ID: ' ), 'wp_review_id_metabox', 'comment', 'normal', 'high' );

}}
add_action( 'add_meta_boxes_comment', 'wp_review_id_metabox_full' );


function wp_review_id_metabox( $comment ) { ?>

           <input type="text" name="new_user_id" value="<?php echo $comment->user_id; ?>"/>
<?php }


function wp_review_id_save_comment( $comment ) {

    $comment['user_id'] = $comment['new_user_id'];

    return $comment;
}
add_filter( 'wp_update_comment_data', 'wp_review_id_save_comment' );
| improve this answer | |

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.