46

I'm creating a user programmatically like this:

$newUser = array(
  'name' => $mail, 
  'pass' => 'password',  // note: do not md5 the password
  'mail' => $mail, 
  'status' => 1, 
  'init' => $mail,
  'roles' => array(5)
);
$user = user_save(null, $newUser);

I have got a role with the role ID equal to five; when I create the user, in the table "users_roles" there is only a row with the value 0 for the role ID, but if I print the user object with var_dump(), it looks like the roles are created.

What am I doing wrong?

  • It only checks for truthiness on the value, the key is what matters. array($role_id => 'anything') – RaisinBranCrunch Jul 31 at 16:06
44

This code worked for me:

$new_user = array(
  'name' => $name,
  'pass' => $sifra, // note: do not md5 the password
  'mail' => $email,
  'status' => 1,
  'init' => $email,
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    3 => 'custom role',
  ),
);

// The first parameter is sent blank so a new user is created.
user_save('', $new_user);
  • Should i write the password a password like "abc12345" and user_save() will convert into md5? – xstean Nov 18 '15 at 20:02
  • 1
    user_save will take care of it – Gulok Feb 16 '17 at 5:01
  • As long as the password is in user_save()'s second argument, it will get hashed. If you do user_save($account) without the $edit argument, then md5 conversion doesn't happen. The answer by @SMTF below demonstrates this. – alec Jan 11 at 15:59
13

This is an example I found on a site.

$account = new stdClass;
$account->is_new = TRUE;
$account->name = 'foo';
$account->pass = user_hash_password('bar');
$account->mail = 'foo@example.com';
$account->init = 'foo@example.com';
$account->status = TRUE;
$account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
$account->timezone = variable_get('date_default_timezone', '');
user_save($account);
  • Sorry, I think the DRUPAL_AUTHENTICATED_RID => TRUE seems wrong, and I don't think user_hash_password is needed. – stefgosselin Mar 16 '16 at 16:43
  • user_hash_password('bar'); is wrong – Joel James Nov 7 '16 at 8:43
  • There are some instances where user_save() fails to hash passwords before saving them (in my own experience and I see others around the web also experiencing it occasionally). I don't know why (but I'd like to!). In these cases calling user_hash_password() seems necessary. – alec Jan 10 at 19:05
  • The code in this answer does require user_hash_password() to be called manually. This is because it's not passing $edit to the user_save(). If the code includes $edit = array('pass' => 'bar'); (which this answer does not) then instead of doing $account->pass = user_hash... you can do user_save($account, $edit); and user_save() will do the hashing for you. – alec Jan 11 at 15:53
13

To programmatically create a user with roles and custom field values (like e.g. First Name and Last Name) you can use the following code:

$new_user = array(
  'name' => 'xgramp',
  'pass' => 'idontwantnoonebutyoutoloveme',
  'mail' => 'xgparsons@flyingburritobrothers.la',
  'signature_format' => 'full_html',
  'status' => 1,
  'language' => 'en',
  'timezone' => 'America/Los_Angeles',
  'init' => 'Email',
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    6 => 'member', // role id for custom roles varies per website
  ),
  'field_first_name' => array(
    'und' => array(
      0 => array(
        'value' => 'Gram',
      ),
    ),
  ),
  'field_last_name' => array(
    'und' => array(
      0 => array(
        'value' => 'Parsons',
      ),
    ),
  ),
);

$account = user_save(NULL, $new_user);

See this blog post and the comments for more details: http://codekarate.com/blog/create-user-account-drupal-7-programmatically

  • You missed a bracket on the roles array – SeanJA Feb 27 '14 at 17:55
2

To create a user programmatically from an e-mail address, try this:

// Use the e-mail address prefix as a user name.
$name = substr($mail, 0, strpos($mail, '@'));

// Make sure the user name isn't already taken.
$query = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->condition('u.name', $name)
  ->execute();
$result = $query->fetch();

// If the user name is taken, append a random string to the end of it.
if ($result->uid) { $name .= '-' . user_password(); }

// Build the user account object and then save it.
$account = new stdClass();
$account->name = $name;
$account->mail = $mail;
$account->init = $mail;
$account->pass = user_password();
$account->status = 1;
user_save($account);
if ($account->uid) {
  drupal_set_message('Created new user with id %uid', array('%uid' => $account->uid));
}
0

To create a user programmatically:

$newUser = array(
      'name' => 'theUserName,
      'pass' => 'thePassWord',
      'mail' => 'the@mail.com',
      'status' => 1,
      'roles' => array(DRUPAL_AUTHENTICATED_RID => 'authenticated user'),
      'init' =>  'the@mail.com',
  );
user_save(null, $newUser);

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.