将自定义 WooCommerce 注册字段添加到管理员 WordPress 用户联系字段

Add custom WooCommerce registration fields to admin WordPress user contact fields

我为此自定义了 WooCommerce 的用户注册流程 我添加了一些额外的字段,例如 出生日期和手机号码。

我添加了这样的表单元素:

function my_extra_register_fields() {?>
    <p class="form-row form-row-wide">
    <label for="reg_dob"><?php _e( 'Date of Birth', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="reg_customer_dob" id="reg_customer_dob"  />
    </p>
    
 
    <p class="form-row form-row-wide">
    <label for="reg_billing_phone"><?php _e( 'Mobile', 'woocommerce' ); ?><span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone"  />
    </p>

   <div class="clear"></div>
    <?php
}
add_action( 'woocommerce_register_form', 'my_extra_register_fields' );

我正在像这样存储数据 -

function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
    
      if ( isset( $_POST['reg_customer_dob'] ) ) {
        // Phone input filed which is used in WooCommerce
        update_user_meta( $customer_id, 'customer_dob', sanitize_text_field( $_POST['reg_customer_dob'] ) );
       }           
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

现在我正在尝试在用户菜单的编辑用户页面中 add/show 这两个详细信息(出生日期和手机号码),以便管理员可以看到详细信息。 附上位置图片

但是我无法读取此页面中的数据。


我正在尝试使用以下钩子:

add_action( 'edit_user_profile', 'print_custom_fileds_in_edit_user', 30 ); 

有什么建议吗?

要显示联系信息之间的字段,您可以使用 user_contactmethods 过滤器挂钩 edit_user_profile

您只需确保用户元键匹配

/**
 * Filters the user contact methods.
 *
 * @since 2.9.0
 *
 * @param string[] $methods Array of contact method labels keyed by contact method.
 * @param WP_User  $user    WP_User object.
 */
function filter_user_contactmethods( $methods, $user ) {    
    // Add user contact methods
    $methods['customer_dob']   = __( 'Date of Birth', 'your-theme-slug' );
    
    return $methods;
}
add_filter( 'user_contactmethods', 'filter_user_contactmethods', 10, 2 );