Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahdiyazdani/a967d204d88c57ceb68d9ac90d1aa98a to your computer and use it in GitHub Desktop.
Save mahdiyazdani/a967d204d88c57ceb68d9ac90d1aa98a to your computer and use it in GitHub Desktop.
Add confirm password field into WooCommerce registration form
<?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' );
@FrancoStino
Copy link

Good Job!!

@immdraselkhan
Copy link

Thank you!

@enkr1
Copy link

enkr1 commented Oct 13, 2020

Thank you!

@jashirouprotips
Copy link

Wonderful...

@DanDoW
Copy link

DanDoW commented Nov 25, 2021

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' ); ?>&nbsp; <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