wp_hash_password( string $password ): string
- Since
- 2.5.0, 6.8.0
- Source
wp-includes/pluggable.php:2747
Description
For integration with other applications, this function can be overwritten to instead use the other package password hashing algorithm.
Compatibility
- WordPress
- since 6.8.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
$passwordstring- Plain text user password to hash.
Return value
string- The hash string of the password.
Performance profile
How much work a call to wp_hash_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
- Trivial
- Scaling
- Constant
- Instructions
- 8–38
- Plugin surface
- 2 hooks
- Called by
- 3
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 50.
Third-party callbacks on 'wp_hash_password_algorithm', 'wp_hash_password_options' run inside this call, and their cost is not bounded by anything here.
3 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
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_hash_password() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($wp_hasher) | 8 | none |
!empty($wp_hasher) | 9 | ->HashPassword() |
empty($wp_hasher) && $algorithm !== "2y" | 26 | apply_filters(), apply_filters(), password_hash() |
empty($wp_hasher) && $algorithm === "2y" | 38 | apply_filters(), apply_filters(), hash_hmac(), base64_encode(), password_hash() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 8–38 | 3 | |
| 8.5 | 50 | 8–38 | 3 | |
| 8.4 | 50 | 8–38 | 3 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 54 | 8–40 | 3 | |
| 8.2 | 54 | 8–40 | 3 | |
| 8.1 | 54 | 8–40 | 3 | |
| 7.4 | 54 | 8–40 | 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 · 2
2 hooks fire while wp_hash_password() runs, in this order:
- apply_filters( wp_hash_password_algorithm )filterline 2784 (+37 into the body)
Filters the hashing algorithm to use in the password_hash() and password_needs_rehash() functions.
- apply_filters( wp_hash_password_options )filterline 2801 (+54 into the body)
Filters the options passed to the password_hash() and password_needs_rehash() functions.
Uses · 1
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- wp_insert_user()Inserts a user into the database.
- wp_set_password()Updates the user's password with a new hashed one.
- wp_update_user()Updates a user in the database.
Source code
function wp_hash_password( #[\SensitiveParameter] $password ) { global $wp_hasher; if ( ! empty( $wp_hasher ) ) { return $wp_hasher->HashPassword( trim( $password ) ); } if ( strlen( $password ) > 4096 ) { return '*'; } /** * Filters the hashing algorithm to use in the password_hash() and password_needs_rehash() functions. * * The default is the value of the `PASSWORD_BCRYPT` constant which means bcrypt is used. * * **Important:** The only password hashing algorithm that is guaranteed to be available across PHP * installations is bcrypt. If you use any other algorithm you must make sure that it is available on * the server. The `password_algos()` function can be used to check which hashing algorithms are available. * * The hashing options can be controlled via the {@see 'wp_hash_password_options'} filter. * * Other available constants include: * * - `PASSWORD_ARGON2I` * - `PASSWORD_ARGON2ID` * - `PASSWORD_DEFAULT` * * The values of the algorithm constants are strings in PHP 7.4+ and integers in PHP 7.3 and earlier. * * @since 6.8.0 * * @param string|int $algorithm The hashing algorithm. Default is the value of the `PASSWORD_BCRYPT` constant. */ $algorithm = apply_filters( 'wp_hash_password_algorithm', PASSWORD_BCRYPT ); /** * Filters the options passed to the password_hash() and password_needs_rehash() functions. * * The default hashing algorithm is bcrypt, but this can be changed via the {@see 'wp_hash_password_algorithm'} * filter. You must ensure that the options are appropriate for the algorithm in use. * * The values of the algorithm constants are strings in PHP 7.4+ and integers in PHP 7.3 and earlier. * * @since 6.8.0 * * @param array $options Array of options to pass to the password hashing functions. * By default this is an empty array which means the default * options will be used. * @param string|int $algorithm The hashing algorithm in use. */ $options = apply_filters( 'wp_hash_password_options', array(), $algorithm ); // Algorithms other than bcrypt don't need to use pre-hashing. if ( PASSWORD_BCRYPT !== $algorithm ) { return password_hash( $password, $algorithm, $options ); } // Use SHA-384 to retain entropy from a password that's longer than 72 bytes, and a `wp-sha384` key for domain separation. $password_to_hash = base64_encode( hash_hmac( 'sha384', trim( $password ), 'wp-sha384', true ) ); // Add a prefix to facilitate distinguishing vanilla bcrypt hashes. return '$wp' . password_hash( $password_to_hash, $algorithm, $options ); }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.