Change the password strength meter labels

Change the password strength meter labels

A while ago I wrote a post about how to remove the password strength meter from the Checkout page. Since I strongly encourage you to not remove it though, like mentioned in that article as well, just changing the meter labels to something easier to understand can help instead!

By changing the meter labels you can explain what could be wrong with the password used, even if that would be hard since the script uses a 3rd-party tool to check the strength of a password which does not only check its length, presence of specific characters, numbers or signs.

You can read more about how it works on WP Tavern.

Probably you already understood that the password strength meter script is not from WooCommerce, so we have to change the meter labels in WordPress directly.

There’s a way to do it, you need to re-localize the script in your theme.
Since the theme is loaded after the core of WordPress, the new localization will load after the one in the core, and this last one will be used instead.

Open your functions.php file located in wp-content/themes/your-child-theme-name/ and add this code at the end of it:


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' )
) );
}

view raw

functions.php

hosted with ❤ by GitHub

Now these new meter labels will be used instead of the core meter labels. They will be used everywhere the password strength meter is used, so in the edit user screen in the Dashboard, in the My Account page if the registration form is enabled and on the checkout page as well.

Update 23rd Feb. 2016: You can change also the default messages returned by WooCommerce. To do so, use this code:


add_filter( 'woocommerce_get_script_data', 'my_strength_meter_custom_strings', 10, 2 );
function my_strength_meter_custom_strings( $data, $handle ) {
if ( 'wc-password-strength-meter' === $handle ) {
$data_new = array(
'i18n_password_error' => esc_attr__( 'Come on, enter a stronger password.', 'theme-domain' ),
'i18n_password_hint' => esc_attr__( 'The password should be at least seven characters long.', 'theme-domain' )
);
$data = array_merge( $data, $data_new );
}
return $data;
}

view raw

functions.php

hosted with ❤ by GitHub

Update 12th Feb. 2018: The code above only works from WooCommerce 3.3+. If you are running an older version of WooCommerce you should use this script instead.


More Posts That You Might Like…


36 responses to “Change the password strength meter labels”

  1. […] Mustone posted a tutorial on changing the WordPress password strength meter messages, which is helpful for WooCommerce […]

  2. Derek Scambler Avatar

    This is great, thank you!

    1. You’re welcome Derek!

  3. Is it possible to adjust the actual strength requirements, e.g. the number of required characters, whether numerals/special characters are needed? – I’ve been having customers that are battling the strength meter. It was costing sales so I temporarily disabled it… But I don’t want it removed for the long term.

    1. This is what I am also looking for. Any ideas how to accomplish this?

      1. Yes, you can use this code to change the strength (default is 3):

        add_filter( 'woocommerce_min_password_strength', create_function( '', 'return 2;' ) );
        
        1. Thank you for the quick reply. That worked just fine.

        2. You’re welcome Alex!

  4. Sir, the above code is gor what because the previous code is well it removes the strong validation but doesn’t suggest to keep strong password. If the above code purpose is to show alert messages to keep strong password with the previous code then it doesn’t work at all.
    So please tell how to add recommendation message for user to keep strong password with previous snippets

  5. I tried doing the same and now I’m getting this error

    Parse error: syntax error, unexpected end of file in /home/soulpuresoaps/public_html/wp-content/themes/azera-shop/functions.php on line 726

    Im a new with WordPress and now I’m completely clueless.

    1. Hi Uttara,
      Sorry for the delay, I don’t know how I missed your comment.

      That error means that you added the code in the wrong location probably.

      If you file ends with ?> make sure to write the code before that sign.

  6. Thank you! We are now using these snippets on http://www.SkyRooms.IO where indeed, we’ve had complaints that checkout was exceptionally hard to do.

    1. You’re welcome Andy! I’m happy I made your life easier!

  7. Hi Nicola, is there a way to determine when the password hint will appear? I have used your code from another post that has allowed me to reduce the minimum strength rating down to a rating of 1 instead of the default 3 but the hint still appears when the user is good to go on a weak password. It disappears when you hit medium strength but would like it to go on weak. Thanks!

  8. […] Update 16th Feb. 2016: Do you want to change only its labels? Check this post! […]

  9. Thank you so much for this code!! I have no formal training in coding at all and have been learning to piece together my business’ website with tutorials like these. I still cannot figure out how to access the .php files for the life of me, but even better, I used the WordPress plugin “Snippets” and it all worked instantly. I had even tried the two WooCommerce account apps beforehand, and each one had major shortcomings this code fixes instantly. Also, if anyone is wondering, you can set the password slot to accept weak passwords (but still reject very weak ones) by setting the value in the code snippet linked above to .5.

    1. Hi Eric,
      Thanks for sharing your thoughts!

      To access PHP files, you can login into your site via FTP. If you don’t know how to do this it would be better to check with your host. Most of them have tutorials about how to use FTP with them.

  10. Thank you. This is of great help

    1. You’re welcome Uzo!

  11. I used this fabulous code on my WooCommerce site and it has worked for over a year, but I updated to WooCommerce 3.3 on a staging copy and started getting error “Warning: array_merge(): Argument #1 is not an array in /home/simpl131/staging/1/wp-content/themes/DiviChild_SCS/functions.php on line 136”

    Line 136 is this code “return array_merge( $data, $data_new );”

    I tried looking at the WooCommerce change log and I am not savvy enough to figure it out what changed. So, I am hoping you have some insight or I will just have to remove the code. Thank you!

    1. Hi Sandy,
      Thanks for reporting this issue. I updated the snippet in the article above for WooCommerce 3.3.

  12. The same issue as Sandy has mentioned above has just happened to me too after my site was automatically updated by my server. Any help resolving this would be much appreciated.

    1. Hi, I updated the snippet in the article above for WooCommerce 3.3.

      1. Perfect, thanks so much for this!

        1. You’re welcome!

  13. Hi,
    I have the same issue that Sandy and Derek mentioned before. Any solution for the “return array_merge( $data, $data_new );”.
    Regards

    1. Hi, I updated the snippet in the article above for WooCommerce 3.3.

  14. Same issue, seems #data is not an array and it needs to be. Tried some things, but http://php.net/manual/en/function.array-merge.php indicates that its always been that way, so WooCommerce must have changed something.

  15. According to https://raw.githubusercontent.com/woocommerce/woocommerce/master/CHANGELOG.txt Woo has not touched the wc_password_strength_meter_params in awhile, although it has been deprecated it should still get handled per https://docs.woocommerce.com/wc-apidocs/class-WC_Deprecated_Filter_Hooks.html. But, I am also not smart enough to know what happened.

  16. Just wondering if anyone found a solution to the “array_merge(): Argument #1 is not an array” issue with the latest version of Woo.

    1. Hi, I updated the snippet in the article above for WooCommerce 3.3.

  17. Thanks! Great work you do as always.

    1. You’re welcome! 🙂

  18. Thank you – exactly what I’m looking for! 2 question: For the first code snippet, I see 2 javascript variables named pwsL10n on the page – and the code is using the first default woocommerce one – so this is not working. The second hook woocommerce_get_script_data does not seem to working at all for me – could it be because I am using the wp_print_scripts filter to enqueue the wc-password-strength-meter.js file?

    1. Hi Adina,

      For the first code snippet, I see 2 javascript variables named pwsL10n on the page – and the code is using the first default woocommerce one – so this is not working

      I don’t understand what you mean. What page are you talking about?

      The second hook woocommerce_get_script_data does not seem to working at all for me – could it be because I am using the wp_print_scripts filter to enqueue the wc-password-strength-meter.js file?

      Maybe, I’m not sure. Try to disable your snippet and see if just with mine it works.

  19. THANK YOU!! The Warning: array_merge(): Argument #1 was proving tricky to pin down. There was zero documentation about it on any of the Woocommerce resources… do you know what the numbers represent at the end of this code? I feel like the first one may be the position in the woocommerce_get_script_data array, but not sure what the 2 represents.

    add_filter( ‘woocommerce_get_script_data’, ‘my_strength_meter_custom_strings’, 10, 2 );

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: