wp_generate_password() WordPress Function

The wp_generate_password() function is used to generate a random password.

wp_generate_password( int $length = 12, bool $special_chars = true, bool $extra_special_chars = false ) #

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


Description

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


Top ↑

Parameters

$length

(int)(Optional) The length of password to generate.

Default value: 12

$special_chars

(bool)(Optional) Whether to include standard special characters.

Default value: true

$extra_special_chars

(bool)(Optional) Whether to include other special characters. Used when generating secret keys and salts.

Default value: false


Top ↑

Return

(string) The random password.


Top ↑

More Information

This function executes the random_password filter after generating the password.

Normal characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Special characters: !@#$%^&*()

Extra special characters: -_ []{}<>~`+=,.;:/?|


Top ↑

Source

File: wp-includes/pluggable.php

	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
		 * @since 5.3.0 Added the `$length`, `$special_chars`, and `$extra_special_chars` parameters.
		 *
		 * @param string $password            The generated password.
		 * @param int    $length              The length of password to generate.
		 * @param bool   $special_chars       Whether to include standard special characters.
		 * @param bool   $extra_special_chars Whether to include other special characters.
		 */
		return apply_filters( 'random_password', $password, $length, $special_chars, $extra_special_chars );
	}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More