wp_password_needs_rehash( string $hash, string|int $user_id = '' ): bool
- Since
- 6.8.0
- Source
wp-includes/pluggable.php:2911
Description
Passwords are hashed with bcrypt using the default cost. A password hashed in a prior version of WordPress may still be hashed with phpass and will need to be rehashed. If the default cost or algorithm is changed in PHP or WordPress then a password hashed in a previous version will need to be rehashed.
Note that, just like wp_check_password(), this function may be used to check a value that is not a user password. A plugin may use this function to check a password of a different type, and there may not always be a user ID associated with the password.
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 4 of the 5 tracked releases, added in 6.8.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$hashstring- Hash of a password to check.
$user_idstring|intoptional- ID of a user associated with the password.Default:
''
Return value
bool- Whether the hash needs to be rehashed.
Performance profile
How much work a call to wp_password_needs_rehash() 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
- 6–35
- Plugin surface
- 3 hooks
- Called by
- 2
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 40.
Third-party callbacks on 'wp_hash_password_algorithm', 'wp_hash_password_options', 'password_needs_rehash' run inside this call, and their cost is not bounded by anything here.
2 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_password_needs_rehash() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($wp_hasher) | 6 | none |
empty($wp_hasher) && $algorithm === "2y" && !$hash | 29 | apply_filters(), apply_filters(), apply_filters() |
empty($wp_hasher) | 34–35 | apply_filters(), apply_filters(), password_needs_rehash(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 40 | 6–35 | 4 | |
| 8.5 | 40 | 6–35 | 4 | |
| 8.4 | 40 | 6–35 | 4 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 47 | 6–42 | 4 | |
| 8.2 | 47 | 6–42 | 4 | |
| 8.1 | 47 | 6–42 | 4 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 48 | 6–43 | 4 |
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 · 3
3 hooks fire while wp_password_needs_rehash() runs, in this order:
- apply_filters( wp_hash_password_algorithm )filterline 2919 (+8 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 2922 (+11 into the body)
Filters the options passed to the password_hash() and password_needs_rehash() functions.
- apply_filters( password_needs_rehash )filterline 2944 (+33 into the body)
Filters whether the password hash needs to be rehashed.
Uses · 2
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Used by · 2
- wp_authenticate_email_password()Authenticates a user using the email and password.
- wp_authenticate_username_password()Authenticates a user, confirming the username and password are valid.
Source code
function wp_password_needs_rehash( $hash, $user_id = '' ) { global $wp_hasher; if ( ! empty( $wp_hasher ) ) { return false; } /** This filter is documented in wp-includes/pluggable.php */ $algorithm = apply_filters( 'wp_hash_password_algorithm', PASSWORD_BCRYPT ); /** This filter is documented in wp-includes/pluggable.php */ $options = apply_filters( 'wp_hash_password_options', array(), $algorithm ); $prefixed = str_starts_with( $hash, '$wp' ); if ( ( PASSWORD_BCRYPT === $algorithm ) && ! $prefixed ) { // If bcrypt is in use and the hash is not prefixed then it needs to be rehashed. $needs_rehash = true; } else { // Otherwise check the hash minus its prefix if necessary. $hash_to_check = $prefixed ? substr( $hash, 3 ) : $hash; $needs_rehash = password_needs_rehash( $hash_to_check, $algorithm, $options ); } /** * Filters whether the password hash needs to be rehashed. * * @since 6.8.0 * * @param bool $needs_rehash Whether the password hash needs to be rehashed. * @param string $hash The password hash. * @param string|int $user_id Optional. ID of a user associated with the password. */ return apply_filters( 'password_needs_rehash', $needs_rehash, $hash, $user_id ); }Changelog
Introduced in 6.8.0. Unchanged from 6.8.8 through 7.1.0.
Signature, return type and hooks compared across 4 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.