作为替代选项,您可以使用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获取用户信息来自动更新用户信息。