wp_generate_password( int $length = 12, bool $special_chars = true, bool $extra_special_chars = false ): string
- Since
- 2.5.0
- Source
wp-includes/pluggable.php:2960
Description
Uses wp_rand() to create passwords with far less predictability than similar native PHP functions like rand() or mt_rand().
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$lengthintoptional- The length of password to generate. Default 12.Default:
12 $special_charsbooloptional- Whether to include standard special characters.
Default true.Default:true $extra_special_charsbooloptional- Whether to include other special characters.
Used when generating secret keys and salts. Default false.Default:false
Return value
string- The random password.
Performance profile
How much work a call to wp_generate_password() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 19–21
- Plugin surface
- 1 hook
- Called by
- 22
Reads stored settings via get_transient(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.
Third-party callbacks on 'random_password' run inside this call, and their cost is not bounded by anything here.
22 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - transienttransient
get_transient()one call below wp_generate_password()
Further down the call graph this can also reach cache, option, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_generate_password() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$i | 19–21 | apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 31 | 19–21 | 3 | |
| 8.5 | 31 | 19–21 | 3 | |
| 8.4 | 31 | 19–21 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 34 | 19–21 | 3 | |
| 8.2 | 34 | 19–21 | 3 | |
| 8.1 | 34 | 19–21 | 3 | |
| 7.4 | 34 | 19–21 | 3 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_generate_password() runs, in this order:
- apply_filters( random_password )filterline 2985 (+25 into the body)
Filters the randomly-generated password.
Uses · 2
- wp_rand()Generates a random non-negative number.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 22
- WP_Application_Passwords::create_new_application_password()Creates a new application password.
- WP_Recovery_Mode_Cookie_Service::generate_cookie()Generates the recovery mode cookie value.
- WP_Recovery_Mode_Cookie_Service::recovery_mode_hash()Gets a form of `wp_hash()` specific to Recovery Mode.
- WP_Recovery_Mode_Key_Service::generate_and_store_recovery_mode_key()Creates a recovery mode key.
- WP_Recovery_Mode_Key_Service::generate_recovery_mode_token()Creates a recovery mode token.
- WP_Session_Tokens::create()Generates a session token and attaches session information to it.
- display_setup_form()Displays installer setup form.
- generate_random_password()Generates a random password.
- get_password_reset_key()Creates, stores, then returns a password reset key for user.
- get_post_embed_html()Retrieves the embed code for a specific post.
- network_step2()Prints step 2 for Network installation process.
- register_new_user()Handles registering a new user.
Show all 22
- wp_ajax_generate_password()Handles generating a password via AJAX.
- wp_ajax_nopriv_generate_password()Handles generating a password in the no-privilege context via AJAX.
- wp_filter_oembed_result()Filters the given oEmbed HTML.
- wp_generate_block_templates_export_file()Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
- wp_generate_user_request_key()Returns a confirmation key for a user action and stores the hashed version for future comparison.
- wp_install()Installs the site.
- wp_privacy_generate_personal_data_export_file()Generate the personal data export file.
- wp_salt()Returns a salt to add to hashes.
- wp_tempnam()Returns a filename of a temporary unique file.
- wpmu_activate_signup()Activates a signup.
Source code
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 * @since 5.3.0 Added the `$length`, `$special_chars`, and `$extra_special_chars` parameters. * * @param string $password The generated password. * @param int $length The length of password to generate. * @param bool $special_chars Whether to include standard special characters. * @param bool $extra_special_chars Whether to include other special characters. */ return apply_filters( 'random_password', $password, $length, $special_chars, $extra_special_chars ); }Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.