Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- }
- wp_enqueue_script( 'password-strength-meter' );
- <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>
- 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
- );
- }
- );
- });
- 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;
- }
- 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;
- }
- 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' )
- ) );
- }
Add Comment
Please, Sign In to add comment