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

  1. if ( !function_exists( 'wp_generate_password' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // Optional. The length of password to generate. Default 12. 
  5. $length = 12; 
  6.  
  7. // Optional. Whether to include standard special characters. 
  8. // Default true. 
  9. $special_chars = true; 
  10.  
  11. // Optional. Whether to include other special characters. 
  12. // Used when generating secret keys and salts. Default false. 
  13. $extra_special_chars = false; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = wp_generate_password($length, $special_chars, $extra_special_chars); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) { 
  2. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
  3. if ( $special_chars ) 
  4. $chars .= '!@#$%^&*()'; 
  5. if ( $extra_special_chars ) 
  6. $chars .= '-_ []{}<>~`+=, .;:/?|'; 
  7.  
  8. $password = ''; 
  9. for ( $i = 0; $i < $length; $i++ ) { 
  10. $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1); 
  11.  
  12. /** 
  13. * Filters the randomly-generated password. 
  14. * @since 3.0.0 
  15. * @param string $password The generated password. 
  16. */ 
  17. return apply_filters( 'random_password', $password );