I want to keep a check if the user has a particular password, so I have been trying it with wp_check_password but the account for which it is checked gets logged out and can’t login again till there is a call of wp_check_password in the code.

Digging into the code, I found out that it sets the password by using the new hash. and moreover if I am using wp_check_password( 'hello', md5('hello'), 1 );, it doesn’t even check what is inside the database and returns true. Isn’t that a bug?

Any ideas how can I check the user’s password?

2 Answers
2

Your example works correctly. You are checking if password hello matches hashed hello – which it naturally does.

Hadn’t thought it through. Your example causes following issue:

  1. You check if hello matches md5 of hello (instead of hash from user’s profile).
  2. It does and then WP thinks this is correct, but outdated md5 hash – that must be updated.
  3. It re-hashes hello and updates user with it, locking him out (since his password is now hello instead of whatever it was before).

See wp_authenticate_username_password() function for extensive example, but basic idea is:

$userdata = get_user_by('login', $username);
$result = wp_check_password($password, $userdata->user_pass, $userdata->ID);

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *