wppaste
WordPress

wp_password_needs_rehash( string $hash, string|int $user_id = '' ): bool

Since
6.8.0
Source
wp-includes/pluggable.php:2911
Checks whether a password hash needs to be rehashed.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–35

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

Plugin surface
3 hooks

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.

Called by
2

2 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

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.

WhenInstructionsCalls it makes
!empty($wp_hasher)6none
empty($wp_hasher) && $algorithm === "2y" && !$hash29apply_filters(), apply_filters(), apply_filters()
empty($wp_hasher)34–35apply_filters(), apply_filters(), password_needs_rehash(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev406–354
8.5406–354
8.4406–3547 fewer instructions than PHP 8.3
8.3476–424
8.2476–424
8.1476–4241 fewer instruction than PHP 7.4
7.4486–434

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:

  1. 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.

  2. 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.

  3. apply_filters( password_needs_rehash )filterline 2944 (+33 into the body)

    Filters whether the password hash needs to be rehashed.

Uses · 2

Used by · 2

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.

  1. 6.8.8
  2. 6.9.7
  3. 7.0.4
  4. 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.