How to add user to WordPress

Last Updated:February 23, 2024
  $user_data = array(
         'user_login' => $username, 
         'user_pass' => $password, 
         'user_email' => $email,     
         'user_registered' => date('Y-m-d H:i:s'), 
         'role' => 'subscriber', 
         'first_name' => $name, 
   );
        
   // Insert user data into the wp_users table
   $user_id = wp_insert_user($user_data);
        
   // Check if user was successfully added
   if (!is_wp_error($user_id)) {
          echo 'User created successfully. ID: ' . $user_id."<br>";
   } else {
          echo 'Error creating user: ' . $user_id->get_error_message()."<br>";
   }