I have created a custom registration page in my WordPress site for visitors to sign up. On this page I am collecting user's password and saving it after it doing a password hash, $password_hash = wp_hash_password($password)
. But when I try to login to wp-admin
, it says Incorrect Password.
Here is the code snippet I am using for saving user data:
function process_registration() {
$password = sanitize_text_field( $_POST['signup_password'] );
//Grabbing other values like firstname, lastname etc.
$password_hash = wp_hash_password( $password );
$user_data = array(
//other fields...
'user_pass' => $password_hash,
'role' => 'contributor',
);
$new_user_id = wp_insert_user( $user_data );
if( is_wp_error( $new_user_id ) ) {
//Do some stuff
} else {
// Do something else
}
}
However, users created from wp-admin
panel are able to login fine. Is this not possible to collect user's password and save it with custom registration form and later use the same for login?
I am still new to WordPress.
Update
After looking into some articles and threads I have realized that generating hashed string for even the same text will always be different, and that's why it is not being able to match the hashed password with the current one. If so, how does another user created inside WordPress admin is able to login? Is this using a different approach? If yes, what? But it not, what I am missing?