WordPress Snippets at WPcustoms

Generate a random password function

By using this code snippet you can generate a random password with in this example has 8 letters. It chooses all letters randomly from the $chars variable.


/**
 * Snippet Name: Generate a random password function
 * Snippet URL: http://www.wpcustoms.net/snippets/generate-random-password-function/
 */
 function random_password( $length = 8 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr( str_shuffle( $chars ), 0, $length );
    return $password;
}