This problem seems to be old enough, I’ve searched over the internet for possible solutions but nothing seems to conclude in a successful way to do it.
Here is what I got:
In the theme I have a custom form which postos username, password and email to a script using the following function:
function createSubscriberUser($username, $email, $password) {
$user_id = username_exists( $username );
if ( !$user_id and email_exists($email) == false ) {
$user_id = wp_create_user( $username, $password, $email );
} else {
$hash = __('User already exists. Password inherited.');
}
}
This works great! the user is succesfully created as a subscriber. However When I try to login the newly created user using wp-login.php, I always get that credentials are invalid.
So I used the following snippet just to see what was going on:
$username = $_POST['user'];
$pass = $_POST['pass'];
$user = get_user_by( 'login', $username );
if ( $user && wp_check_password( $pass, $user->data->user_pass, $user->ID) )
echo " That's it";
else
echo " Nope";
I’m always getting a “Nope”… but if I use a user created from the dashboard, I get the “That’s it” message. So my wild guess is that the hashes being generated by wp_create_user are always different from the ones generated by wp_check_password when passing a plain text password as input from the user.
Is there a way to get the same hash? to login from wp-login.php and programmatically as well?
Thanks a lot for your help.
Make sure you are supplying the ‘plain text’ password to the
wp_create_user
function. I had faced the same issue. I was hashing the password and using it in thewp_create_user
.wp_create_user
is much like a wrapper function forwp_insert_user
. See the below source code:wp_insert_user
will generate its own md5 hash.Alternatively you can directly call the
wp_insert_user
like this:Prev answer from 2015… I had the similar issue. Upon creating a user, “password” worked fine, but not on updating user.
The reason is that I was tempted to use
wp_insert_user()
that can also update user dataBUT here is the catch: upon update you need to provide hashed password and NOT plain text as in initial creation case. Its not well docmented, but you can see it in the code
There are two ways to go ON UPADTING an exiting user:
wp_hash_password('plain-text-pass')