【发布时间】:2020-01-31 09:25:04
【问题描述】:
我目前有 wordpress/woocommerce 和 billing_phone valiation 函数(如下提供),可在 My Account -> Account Details、Checkout 等中使用。此函数首先检查该值是否是有效的电话号码,然后检查是否该电话号码已经存在。这是我的插件中一个非常重要的功能,因为我不能重复。
我需要与后端中的前端相同的检查/平衡...我遇到问题的地方是找到最合适的挂钩或挂钩组,以允许我验证管理区域内的 billing_phone 字段和 1 ) 使用您通常会在管理后端看到的错误消息显示错误,并且 2) 不更新字段并显示错误。
我已经尝试过user_profile_update_errors - 可惜它只在更新元数据后提供错误,我找不到有关如何在$error 变量中包含检查的任何信息。我也试过edit_user_profile_update 和show_user_profile 但我不知道如何在下面的函数中添加错误。
function user_admin_validate_billing_phone() {
if ( isset( $_POST['billing_phone'] ) && !empty( $_POST['billing_phone'] ) ) {
if ( !preg_match('/^04[0-9]{8}$/D', str_replace(' ', '', $_POST['billing_phone'] ) ) ) {
// Error: Billing Phone Number is Invalid.
}
$existing_billing_phone = get_users( 'meta_value=' . str_replace(' ', '', $_POST['billing_phone'] ) );
$current_user = wp_get_current_user();
if ( !empty( $existing_billing_phone ) ) {
if ( $current_user->billing_phone != str_replace(' ', '', $_POST['billing_phone'] ) ) {
// Error: Billing Phone Number Already Exists.
}
else {
return;
}
}
}
}
如上所述,我尝试了以下钩子:
add_action( 'show_user_profile', 'user_admin_validate_billing_phone', 90 );
add_action( 'edit_user_profile', 'user_admin_validate_billing_phone', 90 );
add_action( 'personal_options_update', 'user_admin_validate_billing_phone' );
add_action( 'edit_user_profile_update', 'user_admin_validate_billing_phone' );
add_action( 'user_profile_update_errors, 'user_admin_validate_billing_phone', 10, 3 );
...但是错误出现并且字段仍然更改,或者我不知道如何进行适当的错误处理,例如:
$error = new WP_Error();
$error->add( 'error', __( 'Billing Mobile Phone Number already exists.' ) );
对于正确流程的任何帮助或指导将不胜感激。
【问题讨论】:
标签: wordpress woocommerce admin backend