Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save walidmahade/f9e8b8185cb7633aeba842cd18f3b8b4 to your computer and use it in GitHub Desktop.
Save walidmahade/f9e8b8185cb7633aeba842cd18f3b8b4 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment