wp_generate_password
Generates a random password drawn from the defined set of characters.
Description
(string) wp_generate_password( (int) $length = 12, (constant) $special_chars = true, (bool) $extra_special_chars = false );
Returns (string)
The random password.
Parameters (3)
- 0. $length — Optional. (int) =>
12
- The length of password to generate. Default 12.
- 1. $special_chars — Optional. (constant) =>
true
- Whether to include standard special characters. Default true.
- 2. $extra_special_chars — Optional. (bool) =>
false
- Whether to include other special characters. Used when generating secret keys and salts. Default false.
Usage
if ( !function_exists( 'wp_generate_password' ) ) { require_once ABSPATH . WPINC . '/pluggable.php'; } // Optional. The length of password to generate. Default 12. $length = 12; // Optional. Whether to include standard special characters. // Default true. $special_chars = true; // Optional. Whether to include other special characters. // Used when generating secret keys and salts. Default false. $extra_special_chars = false; // NOTICE! Understand what this does before running. $result = wp_generate_password($length, $special_chars, $extra_special_chars);
Defined (1)
The function is defined in the following location(s).
- /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
- *
- * @param string $password The generated password.
- */
- return apply_filters( 'random_password', $password );
- }