I was hoping there was a way to force strong password BuddyPress, but couldn’t find any so I just implemented my own way of doing that.
I don’t think I have to stress the importance of strong password on this blog. Why would it be a different story for BuddyPress? They have a very nice Password Strength indicator I’m pretty sure you could hook into that to force the strong password but I was OK with a simpler implementation. So here it is, just add the following code to your theme’s functions.php.
function lehelmatyus_validation() {
global $bp;
if ( !empty( $_POST['signup_password'] ) )
if ( !valid_pass( $_POST['signup_password'] ) ){
$bp->signup->errors['signup_password'] = __( 'Your password is not strong enough. It needs to be at least 8 characters long, and must contain at least: 1 lowercase character (a-z), 1 uppercase character (A-Z), 1 number (0-9) and 1 special character (!@#..)', 'buddypress' );
}
}
add_action( 'bp_signup_validate', 'lehelmatyus_validation');
function valid_pass($candidate) {
$r1='/[A-Z]/'; //Uppercase
$r2='/[a-z]/'; //lowercase
$r3='/[!@#$%^&*()-_=+{};:,<.>]/'; // whatever you mean by special char
$r4='/[0-9]/'; //numbers
if(preg_match_all($r1,$candidate, $o)<1) return FALSE;
if(preg_match_all($r2,$candidate, $o)<1) return FALSE;
if(preg_match_all($r3,$candidate, $o)<1) return FALSE;
if(preg_match_all($r4,$candidate, $o)<1) return FALSE;
if(strlen($candidate)<8) return FALSE;
return TRUE;
}
We create a function that make sure it’s not empty and passes the password to a validate function, if it fails we send back a nice descriptive error message. We hook this function into “bp_signup_validate” this takes care of the signup process. We have another function that takes care of the actual checks using regular expressions.
It checks in 5 steps:
- at least 1 Uppercase
- at least 1 lowercase
- at least 1 special character
- at least 1 number
- has the length of minimum 8 characters
If all of these pass then we return TRUE value. That’s pretty much it.
You could force strong password BuddyPress by doing it all in only one regular expression if you really want to, but I would much prefer understandable code to fancy code. This makes it much easier to customize.
If you want to customize the password checker just comment out the lines for the checks you don’t want to enforce. For example if you don’t want to check for special characters, comment out the two lines with the variable $r3. Once where it is defined and once when we check for it with the “IF’ statement.
I hope this helped,
Let me know in the comment section!
Cheers
Hi,
Thanks for this tip. It works and it is very useful 🙂
But there is still a problem when a member change the password in his settings. He can select a password with only one digit. How can I implement the modification in this page too ?
Regards,
Olmyster
Hi Olmyster,
Excuse me for the delayed response. I haven’t fixed that yet, but will have to soon. If I get around and fix that I will write to you once more.
If you figured it out in the mean time please share in < pre > < /pre > tags. Waiting for your reply.
For now I could think up a javascript code that would not accept that simple of a password.
Thanks for your comment!
Lehel
Hi Arpad,
the code works great with the registration.
But how does it look now when the user wants to change his password in his profile setting?
Best regards
Hi Nefret,
I have not implemented that but you need to search for something like a password change hook for BuddyPress.
https://buddypress.org/support/topic/is-there-a-password-change-hook/
Looks like there is a profile_update hook you check whats in there and see if you can apply the validation in there.
If you come up with a good solutions please share it with us.
Thanks!
Arpad Lehel Matyus
Hi Heathera,
Do you have any news regarding password change setting?
I’m looking forward to it!
Thank you.
Hi,
No I don’t. I am not sure what are you referring to regarding password change setting. Can you please explain in detail. Or you can check the BuddyPress Forum for what you are looking for. https://buddypress.org/support/
Lehel
hi, this is working absolutely fine, Could you please help me having to resend the verification button for BuddyPress Registration Form.