// Override the WP random password generator with a custom random password.
add_filter( 'random_password', 'my_random_password' );
function my_random_password() {
// Define password length
$len = 15;
// Define character sets (lower, upper, numbers, special, extra).
$chars = array( 'lower', 'upper', 'numbers', 'special' );
// Nothing to change here. Return new value.
return my_password_generator( $chars, $len );
}
/**
* Generate a random password.
*
* This utility function can be used as-is, or you can
* edit/add to the $chars array for custom string sets.
*
* @param array $chars {
* @type string $lower All lowercase characters.
* @type string $upper All uppercase characters.
* @type string $numbers All numbers 0-9,
* @type string $special Common special characters.
* @type string $extra Extra special characters.
* }
* @param $len string The password length to generate.
* @return $password string The generated password.
*/
function my_password_generator( $chars, $len ) {
$available_chars = array(
'lower' => 'abcdefghijklmnopqrstuvwxyz',
'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'numbers' => '0123456789',
'special' => '!@#$%^&*()',
'extra' => '-_ []{}<>~`+=,.;:/?|',
);
$char_string = '';
foreach( $chars as $char_set ) {
$char_string .= $available_chars[ $char_set ];
}
$password = '';
for( $i = 0; $i < $len; $i++ ) {
$password .= substr( $char_string, wp_rand( 0, strlen( $char_string ) - 1 ), 1 );
}
return $password;
}
Override the WP random password generator with custom generator
This post brought to you by RocketGeek, ButlerBlog, and the following: