Created
May 3, 2024 14:45
-
-
Save KaineLabs/7883f5ffbb398d0287be340470395dc1 to your computer and use it in GitHub Desktop.
BuddyPress - Force Users to add Strong Password
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_filter( 'bp_members_validate_user_password', function ($errors, $pass ) { | |
if ( $errors->has_errors() ) { | |
return $errors; | |
} | |
$uppercase_exp = '/[A-Z]/'; | |
$lowercase_exp = '/[a-z]/'; | |
$special_char_exp = '/[!@#$%^&*()-_=+{};:,<.>]/'; | |
$numeric_exp = '/[0-9]/'; | |
if ( preg_match_all( $uppercase_exp, $pass, $o ) < 1 ) { | |
// Check if password has one upper case letter. | |
$errors->add( 'missing_uppercase', __( 'Please make sure you enter a strong password. It should containing uppercase, lowercase, number, and special character', 'buddypress' ) ); | |
} elseif ( preg_match_all( $lowercase_exp, $pass, $o ) < 1 ) { | |
// Check if password has one lower case letter. | |
$errors->add( 'missing_lowercase', __( 'Please make sure you enter a strong password. It should containing uppercase, lowercase, number, and special character', 'buddypress' ) ); | |
} elseif ( preg_match_all( $special_char_exp, $pass, $o ) < 1 ) { | |
// Check if password has one special letter. | |
$errors->add( 'missing_special_char', __( 'Please make sure you enter a strong password. It should containing uppercase, lowercase, number, and special character', 'buddypress' ) ); | |
} elseif ( preg_match_all( $numeric_exp, $pass, $o ) < 1 ) { | |
// Check if password has one numeric letter. | |
$errors->add( 'missing_numeric_char', __( 'Please make sure you enter a strong password. It should containing uppercase, lowercase, number, and special character', 'buddypress' ) ); | |
} elseif ( strlen( $pass ) < 8 ) { | |
// Check if password has certain limit. | |
$errors->add( 'missing_char_limit', __( 'Please make sure you enter a strong password. It should containing uppercase, lowercase, number, and special character', 'buddypress' ) ); | |
} | |
return $errors; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment