wp_check_password()
Checks the plaintext password against the encrypted Password.
The $hash parameter is the encrypted password string (stored in the database), and the $password parameter is the plain text password. The function encodes the transmitted plaintext password and compares the resulting hash with the $hash if they match (coded according to the same principle) the function returns true.
For integration with other applications, this function can be overwritten to instead use the other package password checking algorithm.
Maintains compatibility between the old version and the new cookie authentication protocol using PHPass library.
Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.
Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.
Hooks from the function
Return
true|false
. False, if the $password does not match the hashed password.
Usage
wp_check_password( $password, $hash, $user_id );
- $password(string) (required)
- Plaintext user's password.
- $hash(string) (required)
- Hash of the user's password to check against.
- $user_id(string|int)
- User ID.
Default: ''
Examples
#1 Check the user password
Let's say we have a password in plain text and we need to find out whether this password is the password of the user with ID 3:
$user = get_userdata( 3 ); if( $user ){ $password = 'my-super-pass'; $hash = $user->data->user_pass; if ( wp_check_password( $password, $hash ) ) echo 'This is the user password'; else echo 'Not his password.'; }
Notes
- Global. PasswordHash. $wp_hasher PHPass object used for checking the password against the $hash + $password.
Changelog
Since 2.5.0 | Introduced. |