【问题标题】:Wordpress Validate Billing_Phone in User Admin AreaWordpress 在用户管理区域验证 Billing_Phone
【发布时间】: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_updateshow_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


    【解决方案1】:

    一个例子

    function validate_phone_field(&$errors, $update = null, &$user  = null) {
        if ( empty($_POST['billing_phone']) ) {
            $errors->add('empty_phone', '<strong>ERROR</strong>: Please Enter a phone number');
        }
    }
    add_action( 'user_profile_update_errors', 'validate_phone_field' );
    

    【讨论】:

    • 感谢您的输入 - 我需要进行一些更改,包括我在其他挂钩中的现有验证,并运行此只是为了提供错误。
    【解决方案2】:

    使用此代码。

    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.
                wc_add_notice(__('Billing Phone Number is Invalid.', 'woocommerce'));
    
            }
    
            $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.
                    wc_add_notice(__('Billing Phone Number Already Exists.', 'woocommerce'));
    
                }
                else { 
                    return;
                }
            }
        }
    }
    

    希望对您有所帮助。

    【讨论】:

    • wc_add_notice 会创建函数错误,因为这是用于前端 woocommerce。它不适用于wp-admin/user-edit.php 用户管理区域。
    【解决方案3】:
    add_action('woocommerce_checkout_process', 'wh_phoneValidateCheckoutFields');
    
    function wh_phoneValidateCheckoutFields() {
        $billing_phone = filter_input(INPUT_POST, 'billing_phone');
    
        if (strlen(trim(preg_replace('/^[6789]\d{9}$/', '', $billing_phone))) > 0) {
            wc_add_notice(__('Invalid <strong>Phone Number</strong>, please check your input.'), 'error');
        }
    }
    

    代码在您的活动子主题(或主题)的functions.php 文件中。或者也可以在任何插件 PHP 文件中。

    请注意:默认情况下,WooCommerce 使用 billing_phone 字段来获取电话号码,但如果您已自定义它,则可以将 billing_phone 替换为您的字段名称。

    希望这会有所帮助!

    【讨论】:

    • 我需要 wp-admin/user-edit.php(用户管理员后端)的功能。我相信woocommerce_checkout_process 是用于前端的?
    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多