Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Last active October 31, 2020 12:24
Show Gist options
  • Save rynaldos-zz/e4c34b3f59fe42574eb69435de9fbd04 to your computer and use it in GitHub Desktop.
Save rynaldos-zz/e4c34b3f59fe42574eb69435de9fbd04 to your computer and use it in GitHub Desktop.
[WooCommerce 3.0] Require minimum password length for account registration, password update, and password resets
add_action('woocommerce_process_registration_errors', 'validatePasswordReg', 10, 2 );
function validatePasswordReg( $errors, $user ) {
// change value here to set minimum required password chars
if(strlen($_POST['password']) < 15 ) {
$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
}
// adding ability to set maximum allowed password chars -- uncomment the following two (2) lines to enable that
//elseif (strlen($_POST['password']) > 16 )
//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
return $errors;
}
add_action('woocommerce_save_account_details_errors', 'validateProfileUpdate', 10, 2 );
function validateProfileUpdate( $errors, $user ) {
// change value here to set minimum required password chars
if(strlen($_POST['password_2']) < 15 ) {
$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
}
// adding ability to set maximum allowed password chars -- uncomment the following two (2) lines to enable that
//elseif (strlen($_POST['password_2']) > 16 )
//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
return $errors;
}
add_action('woocommerce_password_reset', 'validatePasswordReset', 10, 2 );
function validatePasswordReset( $errors, $user ) {
// change value here to set minimum required password chars -- uncomment the following two (2) lines to enable that
if(strlen($_POST['password_3']) < 15 ) {
$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
}
// adding ability to set maximum allowed password chars -- uncomment the following two (2) lines to enable that
//elseif (strlen($_POST['password_3']) > 16 )
//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
return $errors;
}
add_action( 'woocommerce_after_checkout_validation', 'minPassCharsCheckout', 10, 2 );
function minPassCharsCheckout( $user ) {
// change value here to set minimum required password chars on checkout page account registration
if ( strlen( $_POST['account_password'] ) < 15 ) {
wc_add_notice( __( 'Password must be at least 15 characters long.', 'woocommerce' ), 'error' );
}
}
// Did this help? Donate me some BTC: 1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG
@bobafett92
Copy link

Yes, thats my question too. Where to add this code?

@rynaldos-zz
Copy link
Author

rynaldos-zz commented Mar 16, 2018

Yes, add this to the theme's functions.php file or via a plugin that allows code to be added, like https://wordpress.org/plugins/code-snippets/ plugin

Cheers!

@bobafett92
Copy link

thank you...

@bobafett92
Copy link

not working. still not able to set a password with less than 12 chars. Doesnt matter if i change password, use forget password or create new account. nothing working. i use flatsome theme.

@jessequinn
Copy link

@bobafett92. this is to create a minimum requirement. you would need to change the logic to do what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment