以下是处理所需的所有代码:
在前端显示自定义评论表单字段(您已经有了该字段)
在批准的评论中显示电话号码在编辑评论页面的后端编辑电话号码字段在评论列表页面的自定义列中显示电话号码this great tutorial.提交评论时在前端的评论表单上显示自定义元字段
// Display the phone number field on the front end.
add_filter( \'comment_form_default_fields\', \'wpse248957_add_phonenumber_field\' );
function wpse248957_add_phonenumber_field( $fields ) {
$fields[\'phonenumber\'] =
\'<p class="comment-form-phonenumber">\' .
\'<label for="phone-number">Phone Number <span class="required">*</span></label>\' .
\'<input id="phone-number" name="phone-number" type="tel" \' .
\'pattern="[\\+]\\d{2}[\\(]\\d{3}[\\)]\\d{3}[\\-]\\d{4}" \' .
\'title="Phone Number (Format: +63(123)456-7980)" aria-required="true" required="required"/>\' .
\'</p>\';
return $fields;
}
确保指定了电话号码,保存评论时保存评论元数据// Add the filter to check whether the comment meta data has been filled
add_filter( \'preprocess_comment\', \'wpse248957_verify_comment_meta_data\' );
function verify_comment_meta_data( $commentdata ) {
if ( ! isset( $_POST[\'phone-number\'] ) ) {
wp_die( __( \'Error: Please enter your phone number. Hit the Back button \' .
\'on your Web browser and resubmit your comment with a phone number.\', \'text_domain\' )
);
}
return $commentdata;
}
// Save the comment meta data when saving the comment.
add_action( \'comment_post\', \'wpse248957_save_comment_meta_data\' );
function wpse248957_save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST[\'phone-number\'] ) ) && ( $_POST[\'phone-number\'] != \'\') ) {
$phone_number = wp_filter_nohtml_kses( $_POST[\'phone-number\'] );
add_comment_meta( $comment_id, \'phone-number\', $phone_number );
}
}
在评论编辑屏幕上注册并显示元框// Register meta box on comment edit screen
add_action( \'add_meta_boxes_comment\', \'wpse248957_comment_edit_add_meta_box\' );
function wpse248957_comment_edit_add_meta_box() {
add_meta_box( \'title\', __( \'Phone Number\', \'text-domain\' ), \'wpse248957_comment_meta_box\', \'comment\', \'normal\', \'high\' );
}
// Callback function for displaying the comment meta box.
function wpse248957_comment_meta_box( $comment ) {
$phone_number = get_comment_meta( $comment->comment_ID, \'phone-number\', true );
wp_nonce_field( \'wpse248957_comment_fields_update\', \'wpse248957_comment_fields_update\', false );
?>
<p>
<label for="phone-number"><?php _e( \'Phone\', \'text-domain\' ); ?></label>
<input type="text" name="phone-number" value="<?php echo esc_attr( $phone_number ); ?>" class="widefat" />
</p><?php
}
处理更新注释元数据// Update comment meta data from comment editing screen
add_action( \'edit_comment\', \'wpse248957_comment_edit_meta_fields\' );
function wpse248957_comment_edit_meta_fields( $comment_id ) {
if ( ! isset( $_POST[\'wpse248957_comment_fields_update\'] ) ||
! wp_verify_nonce( $_POST[\'wpse248957_comment_fields_update\'], \'wpse248957_comment_fields_update\' )
) {
return;
}
if ( ( isset( $_POST[\'phone-number\'] ) ) && ( $_POST[\'phone-number\'] != \'\' ) ) {
$phone_number = wp_filter_nohtml_kses( $_POST[\'phone-number\'] );
update_comment_meta( $comment_id, \'phone-number\', $phone_number );
} else {
delete_comment_meta( $comment_id, \'phone-number\');
}
}
处理注释列表中的自定义列// Add phone number custom column to comments listing in admin
add_filter( \'manage_edit-comments_columns\' , \'wpse248957_manage_comment_columns\' );
function wpse248957_manage_comment_columns( $columns ) {
return array_merge( $columns,
[ \'phone-number\' => __( \'Phone Number\', \'text_domain\' ) ]
);
}
// Display phone number custom column in comments listing in admin
add_action( \'manage_comments_custom_column\' , \'wpse248957_manage_comments_columns\', 10, 2 );
function wpse248957_manage_comments_columns( $column, $post_id ) {
switch ( $column ) {
case \'phone-number\':
$phone_number = get_comment_meta( $post_id, \'phone-number\', true );
echo esc_html( $phone_number );
break;
}
}
在批准的评论上显示评论元
通过获取电话号码元并在使用的回调函数中回显,可以在批准的评论中显示电话号码wp_list_comments()
:$phone_number = get_comment_meta( $comment->comment_ID, \'phone-number\', true );
echo esc_html( $phone_number );