Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

woocommerce - Minimum number of characters for password, Wordpress

I want to change the default minimum number of characters for password. At the moment I find out the hooks to change the hint and password strength, but nothing about the number of characters (beside some plugin, but I try to avoid them).

At the moment minimum nr. of characters is (9,10) not sure, sometimes works with 9 and sometimes with 10 and I want to make it 8 characters.

question from:https://stackoverflow.com/questions/65914551/minimum-number-of-characters-for-password-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found out a solution. But for that I need to make the password weak (in my case Submit button wasn't active if it wasn't at least medium strength) and after that I add the minimum character number. Also I had to change the hit text.

// Change Password Hint
add_filter( 'password_hint', function( $hint )
{
  return __( 'Hint: The password should be at least 8 characters long. Use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ & ).' );
} );


add_filter( 'woocommerce_min_password_strength', 'reduce_min_strength_password_requirement' );
function reduce_min_strength_password_requirement( $strength ) {
    // 3 => Strong (default) | 2 => Medium | 1 => Weak | 0 => Very Weak (anything).
    return 1; 
}


add_action('woocommerce_process_registration_errors', 'validatePasswordReg', 10, 2 );

function validatePasswordReg( $errors, $user ) {
    // change value here to set minimum required password chars
    if(strlen($_POST['password']) < 8  ) {
        $errors->add( 'woocommerce_password_error', __( 'Password must be at least 8 characters long. Use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).' ) );
    }
    return $errors;
    }

add_action('woocommerce_save_account_details_errors', 'validateProfileUpdate', 10, 2 );

function validateProfileUpdate( $errors, $user ) {
    // change value here to set minimum required password chars
    if(strlen($_POST['password_2']) < 8  ) {
        $errors->add( 'woocommerce_password_error', __( 'Password must be at least 8 characters long. Use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).' ) );
    }
    return $errors;
    }

add_action('woocommerce_password_reset', 'validatePasswordReset', 10, 2 );

function validatePasswordReset( $errors, $user ) {
    // change value here to set minimum required password chars -- uncomment the following two (2) lines to enable that
    if(strlen($_POST['password_3']) < 8  ) {
        $errors->add( 'woocommerce_password_error', __( 'Password must be at least 8 characters long. Use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).' ) );
    }
    return $errors;
    }


add_action( 'woocommerce_after_checkout_validation', 'minPassCharsCheckout', 10, 2 );
function minPassCharsCheckout( $user ) {
    // change value here to set minimum required password chars on checkout page account registration
    if ( strlen( $_POST['account_password'] ) < 8  ) {
        wc_add_notice( __( 'Password must be at least 8 characters long. Use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).', 'woocommerce' ), 'error' );
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...