questions

Unable to login after registration with wp_insert_user

Rate this post

I made a registration form but after the registration I am unable to login into the newly created account. WordPress says wrong password for this email, what am I doing wrong?

Here is the code:

$userdata = array(
    'user_pass'            => $password,
    'user_nicename'        => $company_clean,
    'user_email'           => $email,
    'user_login'           => $company_clean,
    'display_name'         => $company_clean,
    'nickname'             => $company_clean,
    'first_name'           => $name,
    'last_name'            => $last_name,
    'rich_editing'         => false,
    'syntax_highlighting'  => false,
    'comment_shortcuts'    => false,
    'user_registered'      => date('Y-m-d H:i:s'),
    'show_admin_bar_front' => false,
    'role'                 => 'customer',
);

$user_id = wp_insert_user( wp_slash($userdata) );
    
add_user_meta($user_id, 'company', $company, false);
add_user_meta($user_id, 'primary', '1', false);
add_user_meta($user_id, 'miles', 0, false);
add_user_meta($user_id, 'phone', $phone, false);

if(!term_exists($company, 'customers')){
    wp_insert_term(
        $company,
        'customers'
    );
}

wp_redirect(get_home_url() . '/login');

I already tried with and without wp_slash() but the result is the same.

 

✔️Solution:

Solved, before registering the new user I was encrypting the password with md5() and then I was using that to register it.

wp_insert_user() does its own encryption so it was a double encrypted password, hence it was impossible for WP to match the entered value, with the messy one stored in the db.

Anyway searching on the web I found out that it is better to use wp_hash_password() rather than md5().

Leave a Reply

Your email address will not be published.

Back to top button