I am trying to use user_save api. i have passed the following object ($user)

object(stdClass)#30 (6) {
  string(12) "user_name"
  string(22) "myemail@gmail.com"
  array(1) {
    string(10) "OauthUsers"

I have used the following function:

$user = new stdClass();
$user->name = $userdata['name'];
$user->mail = $userdata['email'];
$user->pass = time();
$user->created = time();
$user->status = 1;
$user->roles = array('OauthUsers');

I have tried to drop the "roles" property as well, but in all the cases user_save() returns FALSE.

Do you have any idea of what I am doing wrong?



If you are going to create new users using user_save() then follow the following syntax.

$user = array(
   'name' => $userdata['name'],
   'mail' => $userdata['mail'],
   'init' => $userdata['mail'],
   'pass' => user_password(8),

Now call the function user_save() and pass the user info as second arg.

$account = user_save(null, $user);
     _user_mail_notify('register_admin_created', $account);

To update values for already saved user.

$account = user_load(array('uid' => $userdata['uid']));
$updates = array(
    'pass' => user_password(),
    'name' => 'new_name',
    'roles' => array(
        <rid> => '<role title>';

user_save($account, $updates);
| improve this answer | |
  • what about the role?? i need the added user to have specific role which is "OauthUsers"?? – Alaa Jul 24 '11 at 16:10
  • $user = user_save($user, array('roles' => array('3' => 'special role'))); – Shoaib Nawaz Jul 24 '11 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.