dqkgxq0561
2017-11-29 14:41
浏览 57
已采纳

无法在WordPress中的自定义用户注册中使用手动哈希密码登录WordPress管理员

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxian1895 2017-11-30 05:02
    已采纳
    function process_registration() {
    
        global $wpdb;
        $password_converter=wp_hash_password('tara123451');
            $user_insert_query=$wpdb->insert('wp_users', array(
            'user_login' =>'username1',
            'user_pass' =>$password_converter,
            'user_email' =>'email_id@name.com',
            ));
    
            $user_id = $wpdb->insert_id;
            wp_update_user(array(
            'ID' =>$user_id,
            'role' => 'contributor'
            ));
    }               
    
    点赞 评论

相关推荐 更多相似问题