1 回答
TA贡献1826条经验 获得超6个赞
更新
现在要过滤仅保留数字的电话号码字符串,您可以更好地
preg_replace()
使用str_replace()
.
下面,帐单电话号码在保存之前将被过滤:
WordPress 管理员用户仪表板
我的帐户地址 编辑帐单地址
下订单后(保存数据之前)
代码:
// In Wordpress user profile (increased hook priority)
add_action( 'personal_options_update', 'save_user_billing_phone_fields', 999999 );
add_action( 'edit_user_profile_update', 'save_user_billing_phone_fields', 999999 );
function save_user_billing_phone_fields( $user_id ) {
$field_key = 'billing_phone';
if( isset($_POST[$field_key]) && ! empty($_POST[$field_key]) ) {
// Filtering billing phone (removing everything else than numbers)
$billing_phone = preg_replace( '/[^0-9]/', '', sanitize_text_field($_POST[$field_key]) );
// Update the billing phone
update_user_meta( $user_id, 'billing_phone', $billing_phone );
}
}
// On My account > edit addresses > Billing
add_action( 'woocommerce_process_myaccount_field_billing_phone', 'filter_my_account_billing_phone_fields' );
function filter_my_account_billing_phone_fields( $value ) {
return preg_replace( '/[^0-9]/', '', $value );
}
// On order submission
add_action( 'woocommerce_checkout_posted_data', 'wc_checkout_posted_data_filter_callback' );
function wc_checkout_posted_data_filter_callback( $data ) {
$field_key = 'billing_phone';
if( isset($data[$field_key]) ) {
// Filtering billing phone (removing everything else than numbers)
$data[$field_key] = preg_replace( '/[^0-9]/', '', $data[$field_key] );
}
return $data;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报