A WordPress-centric search engine for devs and theme authors



wp_generate_password ›

Since2.5.0
Deprecatedn/a
wp_generate_password ( $length = 12, $special_chars = true, $extra_special_chars = false )
Parameters: (3)
  • (int) $length Optional. The length of password to generate. Default 12.
    Required: No
    Default: 12
  • (bool) $special_chars Optional. Whether to include standard special characters. Default true.
    Required: No
    Default: true
  • (bool) $extra_special_chars Optional. Whether to include other special characters. Used when generating secret keys and salts. Default false.
    Required: No
    Default: false
Returns:
  • (string) The random password.
Defined at:
Codex:

Generates a random password drawn from the defined set of characters.

Uses wp_rand() is used to create passwords with far less predictability than similar native PHP functions like rand() or mt_rand().



Source

function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		if ( $special_chars ) {
			$chars .= '!@#$%^&*()';
		}
		if ( $extra_special_chars ) {
			$chars .= '-_ []{}<>~`+=,.;:/?|';
		}

		$password = '';
		for ( $i = 0; $i < $length; $i++ ) {
			$password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 );
		}

		/**
		 * Filters the randomly-generated password.
		 *
		 * @since 3.0.0
		 *
		 * @param string $password The generated password.
		 */
		return apply_filters( 'random_password', $password );
	}
endif;

if ( ! function_exists( 'wp_rand' ) ) :