所有客人评论如下user_id = 0
, 而注册用户有其唯一的user_id
附加到注释。如何编辑注释user_id
从注释编辑屏幕。
我想我需要使用edit\\u comment和add\\u meta\\u box\\u comment挂钩。
所有客人评论如下user_id = 0
, 而注册用户有其唯一的user_id
附加到注释。如何编辑注释user_id
从注释编辑屏幕。
我想我需要使用edit\\u comment和add\\u meta\\u box\\u comment挂钩。
谢谢大家的回答。我已经解决了。这是我的密码。我在努力edit_comment
在提问时。
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\' );
您可以从下面的代码中获取参考。它将提供一个选项来更新注释的用户id(https://prnt.sc/s57q4f). 在活动主题的函数中粘贴下面的代码。php文件。我已经测试过了,它对我有效。如果对你有用,请告诉我。
# 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;
}
作为替代选项,您可以使用wp_update_comment_data
在数据库默认更新之前的筛选器。
专业人士
无需执行任何保存机制,数据准备的重点是在保存之前完全控制所有数据add_meta_boxes
而@Chetan Vaghela正在使用add\\u meta\\u box\\u comment。它们在编辑表单注释中的调用位置。php是并排的,所以结果应该是相同的。您还可以通过使用@Chetan Vaghela元框和注释数据过滤器来组合这两种方法wp_update_comment_data
进一步简化解决方案
如果使用add_meta_box function
, 只能使用位置normal
. 它将不会在其他位置渲染。
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;
}
我在一个默认主题中测试了上述代码,并证明是可行的。在更新用户id的同时,您还可以考虑在数据操作期间通过id获取用户信息来自动更新用户信息。我一直在看the documentation 对于wp_list_comments(). 其中一个选项是“short\\u ping”,记录如下:(布尔)是否要使用短ping。“短ping”到底是什么?我为什么要短ping?