admin管理员组文章数量:1384438
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.
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.
Share Improve this question edited Apr 23, 2020 at 17:32 Bikram asked Apr 21, 2020 at 14:01 BikramBikram 3386 silver badges22 bronze badges3 Answers
Reset to default 3 +25You 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;
}
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.
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' );
本文标签: usersHow to edit userid on the comment edit screen
版权声明:本文标题:users - How to edit user_id on the comment edit screen 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535192a2611271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论