在注释表单中添加的自定义字段不会在编辑中显示

时间:2016-12-12 作者:Ruby Ann Rosales

这是我在函数中的代码。用于在注释表单中添加自定义字段的php

add_filter( \'comment_form_default_fields\', \'add_phonenumber_field\' );
function add_phonenumber_field($fields) {
    $fields[\'phonenumber\'] = \'<p class="comment-form-phonenumber"><label for="phonenumber">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;
}
这就是评论表单现在的样子enter image description here

但当我发送评论并单击“评论”选项卡下仪表板中的“编辑”时,没有电话号码字段。enter image description here

当我编辑评论并将其显示在前端已批准的评论中时,如何使其显示在中?谢谢你的帮助!

2 个回复
SO网友:CodeMascot

您需要使用add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); WordPress的功能是将注释额外字段添加到注释编辑中。请have a look here 您可以在下面的"How to add extra fields on comments".

SO网友:Dave Romsey

以下是处理所需的所有代码:

在前端显示自定义评论表单字段(您已经有了该字段)

  • 在批准的评论中显示电话号码
  • 在编辑评论页面的后端编辑电话号码字段
  • 在评论列表页面的自定义列中显示电话号码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 );