wp_check_password( string $password, string $hash, string|int $user_id = '' ): bool
- Since
- 2.5.0, 6.8.0
- Source
wp-includes/pluggable.php:2736
Description
Note that 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.
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- Plaintext password.
$hashstring- Hash of the password to check against.
$user_idstring|intoptional- ID of a user associated with the password.Default:
''
Return value
bool- False, if the $password does not match the hashed password.
Performance profile
How much work a call to wp_check_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
- 22–39
- Plugin surface
- 1 hook
- 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 78.
Third-party callbacks on 'check_password' 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 · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_check_password() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($wp_hasher) | 22 | apply_filters() |
| always | 23–39 | ->CheckPassword(), apply_filters() |
| always | 24 | md5(), hash_equals(), apply_filters() |
empty($wp_hasher) && !$hash | 29 | password_verify(), apply_filters() |
empty($wp_hasher) && $hash | 39 | hash_hmac(), base64_encode(), password_verify(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 78 | 22–39 | 5 | |
| 8.5 | 78 | 22–39 | 5 | |
| 8.4 | 78 | 22–39 | 5 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 87 | 22–45 | 5 | |
| 8.2 | 87 | 22–45 | 5 | |
| 8.1 | 87 | 22–45 | 5 | |
| 7.4 | 87 | 22–45 | 5 |
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_check_password() runs, in this order:
- apply_filters( check_password )filterline 2779 (+43 into the body)
Filters whether the plaintext password matches the hashed password.
Uses · 4
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- new PasswordHash(8, \true)::CheckPassword()
- PasswordHash::__construct()
Used by · 3
- WP_Application_Passwords::check_password()Checks a plaintext application password against a hashed password.
- 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_check_password( #[\SensitiveParameter] $password, $hash, $user_id = '' ) { global $wp_hasher; if ( strlen( $hash ) <= 32 ) { // Check the hash using md5 regardless of the current hashing mechanism. $check = hash_equals( $hash, md5( $password ) ); } elseif ( ! empty( $wp_hasher ) ) { // Check the password using the overridden hasher. $check = $wp_hasher->CheckPassword( $password, $hash ); } elseif ( strlen( $password ) > 4096 ) { // Passwords longer than 4096 characters are not supported. $check = false; } elseif ( str_starts_with( $hash, '$wp' ) ) { // Check the password using the current prefixed hash. $password_to_verify = base64_encode( hash_hmac( 'sha384', $password, 'wp-sha384', true ) ); $check = password_verify( $password_to_verify, substr( $hash, 3 ) ); } elseif ( str_starts_with( $hash, '$P$' ) ) { // Check the password using phpass. require_once ABSPATH . WPINC . '/class-phpass.php'; $check = ( new PasswordHash( 8, true ) )->CheckPassword( $password, $hash ); } else { // Check the password using compat support for any non-prefixed hash. $check = password_verify( $password, $hash ); } /** * Filters whether the plaintext password matches the hashed password. * * @since 2.5.0 * @since 6.8.0 Passwords are now hashed with bcrypt by default. * Old passwords may still be hashed with phpass or md5. * * @param bool $check Whether the passwords match. * @param string $password The plaintext password. * @param string $hash The hashed password. * @param string|int $user_id Optional ID of a user associated with the password. * Can be empty. */ return apply_filters( 'check_password', $check, $password, $hash, $user_id ); }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.8.8 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.