[英]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_action( 'edit_user_profile', 'print_custom_fileds_in_edit_user', 30 );
任何建议?
要显示联系信息之间的字段,您可以使用与edit_user_profile
相对的user_contactmethods
过滤器钩子
您只需要确保用户元键匹配
/**
* 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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.