Require a Minimum Password Length
Rejects new passwords shorter than 12 characters on the profile screen, the Add New User screen and the password reset form.
Opens WPIDE on
Code
$wpide_min_password_length = 12;
add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) use ( $wpide_min_password_length ) {
if ( ! empty( $user->user_pass ) && strlen( $user->user_pass ) < $wpide_min_password_length ) {
$errors->add( 'pass', sprintf( '<strong>Error:</strong> Passwords must be at least %d characters long.', $wpide_min_password_length ) );
}
}, 10, 3 );
add_action( 'validate_password_reset', function ( $errors ) use ( $wpide_min_password_length ) {
$password = isset( $_POST['pass1'] ) && is_string( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '';
if ( '' !== $password && strlen( $password ) < $wpide_min_password_length ) {
$errors->add( 'pass', sprintf( '<strong>Error:</strong> Passwords must be at least %d characters long.', $wpide_min_password_length ) );
}
} );
Notes
Change 12 to your preferred minimum. WooCommerce account forms use their own strength meter.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.