WordPress at a glance

wp_generate_password() WP 2.5.0

Generates a random password. You can specify the length and what characters to use.

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

Это pluggable функция — т.е. её можно заменить из плагина. Это значит, что она будет работать (подключается) только после подключения всех плагинов, а до этого момента функция еще не определена... Поэтому нельзя вызывать эту и зависящие от неё функции прямо из кода плагина. Их нужно вызывать через хук plugins_loaded или позднее, например хук init.

Function replacement (override) — in a plugin you can create a function with the same name, then it replace this function.

Works based on: wp_rand()
✈ 1 time = 0.000068s = very fast | 50000 times = 1.32s = fast | PHP 7.0.4, WP 4.4.2
Hooks from the function
Return

String. The random password.

Usage

$password = wp_generate_password( $length, $special_chars, $extra_special_chars );
$length(int)
Password length. The number of characters in the password.
Default: 12
$special_chars(true/false)
Whether to include standard special characters: !@#$%^&*()
Default: true
$extra_special_chars(true/false)
Whether to include other special characters. Used when generating secret keys and salts: -_ []{}<>~`+=,.;:/?|
Default: false

Examples

#1 Generate a password and display it

Various examples of password generation:

echo wp_generate_password();                 // @iU!ZnjUWZsg
echo wp_generate_password( 15, false );      // YdD6j750MeiOkPa
echo wp_generate_password( 15, true, true ); // .WfvgX6`V^Vg:,_

Changelog

Since 2.5.0 Introduced.

Code of wp generate password: wp-includes/pluggable.php WP 5.2.2

<?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
	 *
	 * @param string $password The generated password.
	 */
	return apply_filters( 'random_password', $password );
}

Related Functions

From tag: password

More from tag: Site security (safety)

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register