wppaste
WordPress

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:2706
Generates a random password drawn from the defined set of characters.

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

Reads stored settings via get_transient(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
19–21

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 31.

Plugin surface
1 hook

Third-party callbacks on 'random_password' run inside this call, and their cost is not bounded by anything here.

Called by
22

22 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • transienttransientget_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.

WhenInstructionsCalls it makes
!$i19–21apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3119–213
8.53119–213
8.43119–2133 fewer instructions than PHP 8.3
8.33419–213
8.23419–213
8.13419–213
7.43419–213

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:

  1. apply_filters( random_password )filterline 2731 (+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

Show all 22

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.