Created
October 8, 2016 19:26
-
-
Save mahdiyazdani/a967d204d88c57ceb68d9ac90d1aa98a to your computer and use it in GitHub Desktop.
Add confirm password field into WooCommerce registration form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add the code below to your theme's functions.php file | |
* to add a confirm password field on the register form under My Accounts. | |
*/ | |
function woocommerce_registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) { | |
global $woocommerce; | |
extract( $_POST ); | |
if ( strcmp( $password, $password2 ) !== 0 ) { | |
return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) ); | |
} | |
return $reg_errors; | |
} | |
add_filter('woocommerce_registration_errors', 'woocommerce_registration_errors_validation', 10, 3); | |
function woocommerce_register_form_password_repeat() { | |
?> | |
<p class="form-row form-row-wide"> | |
<label for="reg_password2"><?php _e( 'Confirm password', 'woocommerce' ); ?> <span class="required">*</span></label> | |
<input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" /> | |
</p> | |
<?php | |
} | |
add_action( 'woocommerce_register_form', 'woocommerce_register_form_password_repeat' ); |
Good Job!!
Thank you!
Thank you!
Wonderful...
Thanks a lot!
In case someone can benefit, I used this changes:
Update woocommerce clases:
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password2"><?php esc_html_e( 'Confirm password', 'impredecible-basoa' ); ?> <span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
</p>
In case you use checkout registration add this:
Adds a confirm password field on the register form
add_filter( 'woocommerce_checkout_fields' , 'pasword_confirmation_field_checkout' );
function pasword_confirmation_field_checkout( $fields ) {
$fields['account']['account_password']['class'] = array( 'form-row-first' );
$fields['account']['account_password_ver'] = array(
'type' => 'password',
'label' => 'Confirm password',
'required' => true,
'class' => array( 'form-row-last' ),
'clear' => true,
'priority' => 999,
);
return $fields;
}
Generate error message if field values are different
add_action('woocommerce_checkout_process', 'matching_password_checkout');
function matching_password_checkout() {
$pssw1 = $_POST['account_password'];
$pssw2 = $_POST['account_password_ver'];
if ( $pssw1 !== $pssw2 ) {
wc_add_notice( "Your passwords doesn't match", 'error' );
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The fields are not adding in the registration form using a plugin. I am trying to add it manually using a code that is in this complete guide https://wpitech.com/add-woocommerce-registration-form-fields/. Is there any alternative to do this? It would be really helpful if you could help me to add fields in the registration form.
function Woo_register_fields() {?>
*