Is it possible to change the labels for "Weak, Medium, Strong" etc... in the Password Indicator that's used in the user profile? I've been asked to change the word "Weak" to "OK" since this level of passwords is acceptable for our subscribers. Is there a filter I can hook into?
5 Answers
Adding this to my function.php file in the child theme folder did it for me:
add_action( 'wp_enqueue_scripts', 'my_strength_meter_localize_script' );
function my_strength_meter_localize_script() {
wp_localize_script( 'password-strength-meter', 'pwsL10n', array(
'empty' => __( 'But... it\'s empty!', 'theme-domain' ),
'short' => __( 'Too short!', 'theme-domain' ),
'bad' => __( 'Not even close!', 'theme-domain' ),
'good' => __( 'You are getting closer...', 'theme-domain' ),
'strong' => __( 'Now, that\'s a password!', 'theme-domain' ),
'mismatch' => __( 'They are completely different, come on!', 'theme-domain' )
) );
}
This post on Web Tips hooks into a few choice actions and filters to modify password strength allowances, but commenters seemed to have some trouble with it, so take this bit with a grain of salt.
// functions.php
add_action( 'user_profile_update_errors', 'validateProfileUpdate', 10, 3 );
add_filter( 'registration_errors', 'validateRegistration', 10, 3 );
add_action( 'validate_password_reset', 'validatePasswordReset', 10, 2 );
/**
* validate profile update
*
* @author Joe Sexton <joe.@webtipblog.com>
* @param WP_Error $errors
* @param boolean $update
* @param object $user raw user object not a WP_User
*/
public function validateProfileUpdate( WP_Error &$errors, $update, &$user ) {
return validateComplexPassword( $errors );
}
/**
* validate registration
*
* @author Joe Sexton <joe.@webtipblog.com>
* @param WP_Error $errors
* @param string $sanitized_user_login
* @param string $user_email
* @return WP_Error
*/
function validateRegistration( WP_Error &$errors, $sanitized_user_login, $user_email ) {
return validateComplexPassword( $errors );
}
/**
* validate password reset
*
* @author Joe Sexton <joe.@webtipblog.com>
* @param WP_Error $errors
* @param stdClass $userData
* @return WP_Error
*/
function validatePasswordReset( WP_Error &$errors, $userData ) {
return validateComplexPassword( $errors );
}
/**
* validate complex password
*
* @author Joe Sexton <joe.@webtipblog.com>
* @param WP_Error $errors
* @param stdClass $userData
* @return WP_Error
*/
function validateComplexPassword( $errors ) {
$password = ( isset( $_POST[ 'pass1' ] ) && trim( $_POST[ 'pass1' ] ) ) ? $_POST[ 'pass1' ] : null;
// no password or already has password error
if ( empty( $password ) || ( $errors->get_error_data( 'pass' ) ) )
return $errors;
// validate
if ( ! isStrongPassword( $password ) )
$errors->add( 'pass', '<strong>ERROR</strong>: Your password must contain at least 8 characters.' ); // your complex password error message
return $errors;
}
/**
* isStrongPassword
*
* @author Joe Sexton <joe.@webtipblog.com>
* @param string $password
* @return boolean
*/
function isStrongPassword( $password ) {
return strlen( $password ) >= 8; // your complex password algorithm
}
If you can't get the hooking route to work or if you have a custom login page, this post on Tuts Plus has a handy tutorial for utilizing the existing script on your own forms.
Add to functions.php
:
wp_enqueue_script( 'password-strength-meter' );
In your HTML:
<form>
<input type="password" name="password" />
<input type="password" name="password_retyped" />
<span id="password-strength"></span>
<input type="submit" disabled="disabled" value="Submit" />
</form>
The script:
function checkPasswordStrength( $pass1,
$pass2,
$strengthResult,
$submitButton,
blacklistArray ) {
var pass1 = $pass1.val();
var pass2 = $pass2.val();
// Reset the form & meter
$submitButton.attr( 'disabled', 'disabled' );
$strengthResult.removeClass( 'short bad good strong' );
// Extend our blacklist array with those from the inputs & site data
blacklistArray = blacklistArray.concat( wp.passwordStrength.userInputBlacklist() )
// Get the password strength
var strength = wp.passwordStrength.meter( pass1, blacklistArray, pass2 );
// Add the strength meter results
switch ( strength ) {
case 2:
$strengthResult.addClass( 'bad' ).html( pwsL10n.bad );
break;
case 3:
$strengthResult.addClass( 'good' ).html( pwsL10n.good );
break;
case 4:
$strengthResult.addClass( 'strong' ).html( pwsL10n.strong );
break;
case 5:
$strengthResult.addClass( 'short' ).html( pwsL10n.mismatch );
break;
default:
$strengthResult.addClass( 'short' ).html( pwsL10n.short );
}
// The meter function returns a result even if pass2 is empty,
// enable only the submit button if the password is strong and
// both passwords are filled up
if ( 4 === strength && '' !== pass2.trim() ) {
$submitButton.removeAttr( 'disabled' );
}
return strength;
}
jQuery( document ).ready( function( $ ) {
// Binding to trigger checkPasswordStrength
$( 'body' ).on( 'keyup', 'input[name=password1], input[name=password2]',
function( event ) {
checkPasswordStrength(
$('input[name=password]'), // First password field
$('input[name=password_retyped]'), // Second password field
$('#password-strength'), // Strength meter
$('input[type=submit]'), // Submit button
['black', 'listed', 'word'] // Blacklisted words
);
}
);
});
The "Weak" text is passed through _x
function, which calls translate_with_gettext_context
, so I would try the following:
add_filter( 'gettext_with_context', 'wpse199813_change_password_indicatior', 10, 4 );
function wpse199813_change_password_indicatior($translations, $text, $context, $domain){
if( $text == "Weak" && $context == "password strength")
return "OK";
return $translations;
}
-
Your code didn't work, but I was able to fix it with the 'ngettext' and 'gettext' functions. Your answer helped point me in that direction. Thanks.– LBFAug 27, 2015 at 17:38
You can do it right in your theme localization file.
Open *.pot
file and create the translations for labels you need, e. g. OK
for Weak
.
See password-strength-meter
around line #366
in /wp-includes/script-loader.php
for the reference (but don't change there anything).
Update:
I've found the solution using jQuery for the similar problem, but you have to simplify that code to use only one password meter: How to use wordpress default Password Strength Meter script. Also see /wp-admin/js/user-profile.js
for reference because that solution is pretty old.
-
Would the theme localization apply only to the theme files? The password strength is in the core. Can you put a .pot file in the core that only translates that one word?– LBFAug 27, 2015 at 16:59
I was able to fix it with this method. Is this good/bad? It's working, but I'd be interested in feedback in case this could have unexpected consequences.
add_filter('gettext', 'translate_text');
add_filter('ngettext', 'translate_text');
function translate_text($translated) {
$translated = str_ireplace('Very Weak', 'Bad', $translated);
$translated = str_ireplace('Weak', 'OK', $translated);
return $translated;
}