wp_generate_password

Advertisement

Summery Summery

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

Syntax Syntax

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

Description Description

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

Parameters Parameters

$length

(Optional) The length of password to generate.

Default value: 12

$special_chars

(Optional) Whether to include standard special characters.

Default value: true

$extra_special_chars

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

Default value: false

Return Return

(string) The random password.

Source Source

File: wp-includes/pluggable.php

	 * than similar native PHP functions like `rand()` or `mt_rand()`.
	 *
	 * @since 2.5.0
	 *
	 * @param int  $length              Optional. The length of password to generate. Default 12.
	 * @param bool $special_chars       Optional. Whether to include standard special characters.
	 *                                  Default true.
	 * @param bool $extra_special_chars Optional. Whether to include other special characters.
	 *                                  Used when generating secret keys and salts. Default false.
	 * @return string The random password.
	 */
	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.

Advertisement

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Advertisement

Leave a Reply