woocommerce中的自定义注册字段未经验证

时间:2014-02-28 07:46:43

标签: wordpress woocommerce

    //Add phone number field to customer registration
add_filter( 'register_form', 'custom_phone_no_registration_field' );
function custom_phone_no_registration_field( ) {
    echo '<div class="form-row form-row-wide"><label for="reg_phone">'.__('Phone Number', 'woocommerce').' <span class="required">*</span></label>
    <input type="text" class="input-text" name="phone" id="reg_phone" size="30" value="'.esc_attr($_POST['phone']).'" /></div>';

}

//Validation registration form  after submission using the filter registration_errors
add_action('registration_errors', 'custom_registration_errors_validation', 10,3);
function custom_registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
    global $woocommerce;
    extract($_POST); // extracting $_POST into separate variables
    if(empty($phone)) :
        $woocommerce->add_error( __( 'Please, fill in all the required fields.', 'woocommerce' ) );
    endif;
    return $reg_errors;
}

我正在使用register_form函数将电话号码字段添加到注册表单。它可以工作,但registration_errors功能不起作用。有人可以帮我解释一下代码吗?

1 个答案:

答案 0 :(得分:1)

我认为您需要在woocommerce_process_registration_errors过滤器上进行验证。请查看process_registration()中的includes/class-wc-form-handler.php方法。

add_action( 'woocommerce_process_registration_errors', 'custom_registration_error' );

function custom_registration_errors( $validation_error ){
    if ( ! isset( $_POST['phone'] ) ){
        $validation_error = new WP_Error( 'phone-error', __( 'Please enter a phone number.', 'your-plugin' ) );
    }
    return $validation_error;
}