I'm attempting to programmatically create a user. I'd like to be able to create the user, then set the current global $user object to this newly created user. Here is what my code looks like.

    $new_user = array(
        'name' => $form_state['values']['username'],
        'pass' => $form_state['values']['password'],
        'mail' => $form_state['values']['email'],
        'status' => 1,
        'init' => $form_state['values']['email'],
        'roles' => array(
            DRUPAL_AUTHENTICATED_RID => 'authenticated user'

    if ($saved_user = user_save(NULL, $new_user)){
        user_load($saved_user->uid, TRUE);
        $form_state['redirect'] = 'member/profile';
    } else {
        $form_state['redirect'] = 'member/join';

The reason for me essentially wrapping the existing user page is that there is a lot of additional functionality needed. This code does create a user in the database, but it fails in two places: 1) It does not create any roles for this new user in the users_roles table, and 2) It does not log the current user in or adjust the global $user object.

Any ideas why this is failing?

EDIT: Using Drupal 7.9

EDIT2: I solved the user not being logged in issue by using the function user_login_submit. Roles still aren't creating correctly, however.

  • What version of Drupal are you using? – Clive Feb 28 '12 at 19:17
  • Sorry, should have specified - Drupal 7.9 – rybosome Feb 28 '12 at 19:23

user_save() does nothing with roles unless they're part of the $account object (first argument passed in). Stepping through the code I can't see any reason why simply setting up a dummy $account var to pass in, just containing the roles, wouldn't work:

$new_user = array(
    'name' => $form_state['values']['username'],
    'pass' => $form_state['values']['password'],
    'mail' => $form_state['values']['email'],
    'status' => 1,
    'init' => $form_state['values']['email']

$account = new stdClass;
$account->roles = array(
  DRUPAL_AUTHENTICATED_RID => 'authenticated user'

if ($saved_user = user_save($account, $new_user)) {
  // etc...

user_save() seems to do all the rest of the required setup on the $account object so this should work fine.

  • While your solution should work fine, I have code that sets the roles on the $new_user and it works without any problems. – dysrama Feb 29 '12 at 11:44
  • @kummel You must be setting them on the $account object as well...have a look at the code for user_save(), specifically the bit which says if (count($account->roles) > 1) {. If $account->roles isn't set, no roles will ever be written to the database – Clive Feb 29 '12 at 11:53
  • No, I'm not -> user_save(null, $new); No account object. Later on in the user_save than the bit you're pointing to it loops the $new data and sets it on $account. foreach ($edit as $key => $value) { $account->$key = $value; } So something else must be going wrong. – dysrama Feb 29 '12 at 12:28
  • If that were true then simply adding an $account object with a roles property wouldn't fix the problem (as it would be overwritten by the loop you've posted), but the OP has confirmed the above has fixed the problem. – Clive Feb 29 '12 at 12:43
  • sorry, the loop happens before the code you're pointing to, not after. – dysrama Feb 29 '12 at 14:46

Drupal's anonymous and authenticated roles don't get saved in the users_roles table and you don't need to add the role yourself, all users are by default authenticated. Try editing your user in the backend, you'll see that it has authenticated checked off. The code you list above where you send the roles array in the $edit will work fine with any other role.

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.