Woocommerce注册表格验证不起作用 [英] Woocommerce registration form validation not working
本文介绍了Woocommerce注册表格验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在woocommerce注册表单中添加了一些新字段,但是我无法验证这些新字段.
I have added some new fields to my woocommerce registration form, but i can't validate these new fields.
有我的新字段
add_action('register_form','myplugin_register_form');
function myplugin_register_form (){
$first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
?>
<div class="row">
<div class="col-md-4">
<label for="first_name">Prénom <span class="required">*</span></label>
<input type="text" class="input-text" name="first_name" id="first_name" value="<?php if ( ! empty( $_POST['first_name'] ) ) echo esc_attr( $_POST['first_name'] ); ?>" />
</div>
<div class="col-md-4">
<label for="last_name">Nom <span class="required">*</span></label>
<input type="text" class="input-text" name="last_name" id="last_name" value="<?php if ( ! empty( $_POST['last_name'] ) ) echo esc_attr( $_POST['last_name'] ); ?>" />
</div>
</div>
<?php
}
这是我根据wordpress Codex进行验证的过滤器 https://codex.wordpress.org/Customizing_the_Registration_Form
And this is my filter for the validation according to the wordpress codex https://codex.wordpress.org/Customizing_the_Registration_Form
function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
if ( empty( $_POST['first_name'] ) )
$errors->add( 'first_name_error', __('<strong>ERROR</strong>: You must include a first name.','mydomain') );
return $errors;
}
当我提交表单时,如果没有$ _POST ['first_name']字段,它会正确无误地通过.
When i submit my form, without the field $_POST['first_name'] it passe without errors.
最好的方法是什么?
谢谢您的帮助
推荐答案
请在主题/子主题的函数中编写此代码.php
Please write this code in theme's/child theme's functions.php
/**
* Validate the extra register fields.
*
* @param string $username Current username.
* @param string $email Current email.
* @param object $validation_errors WP_Error object.
*
* @return void
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['first_name'] ) && empty( $_POST['first_name'] ) ) {
$validation_errors->add( 'first_name_error', __( '<strong>Error</strong>: First Name is required!.', 'woocommerce' ) );
}
if ( isset( $_POST['last_name'] ) && empty( $_POST['last_name'] ) ) {
$validation_errors->add( 'last_name_error', __( '<strong>Error</strong>: Last Name is required!.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
这篇关于Woocommerce注册表格验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文