普通网友 2017-11-29 14:41
浏览 59
已采纳

无法在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'
            ));
    }               
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)