Create a user account in Drupal 6 programmatically

By shane
Wed, 2011-06-08 15:52
11 comments

Share with Others

Looking to do this in Drupal 7? See my updated post on how to Create a user account in Drupal 7 programmatically.

There have been a few situations in which I needed to create a user on a Drupal site based on some action. There are many other situations where this might be necessary so I put together a really quick example of how it can work. The code I have below is based heavily on the Ubercart code that creates new users when they purchase a product on an Ubercart powered Drupal site.

  //the user name of the new user
  $user_name = "example";
 
  //the users email address
  $email = "<a href="mailto:example@example.com">example@example.com</a>";
 
  //set up the user fields using a random 8 character password
  $fields = array(
    'name' => $user_name,
    'mail' => $email,
    'pass' => user_password(8),
    'status' => 1,
  );
 
  //you can give a user roles if necessary based on the role name
  $fields['roles'] = array('test_role');
 
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
 
  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];
 
  // Send the e-mail through the user module.
  drupal_mail('user', 'register_admin_created', $email, NULL, array('account' => $account), variable_get('site_mail', '<a href="mailto:noreply@example..com">noreply@example..com</a>'));

Useful? Let me know what you think.

Comments

I think I can help you out, but I might need a little more information on how you are trying to save the date field. There are 3 different types of date fields (if you are using CCK). These include Datetime, Date, and Datestamp. The type of field you have will affect how the value needs to be saved. Based on the post you commented on, it appears you want to save this date field to a user account that you are creating automatically. If you are storing additional information about a user, you may need to look into the profile module (in Drupal 6 core) or the content profile module. Either that or you could manually create a database table to store the additional information.

If you provide some more information on your situation, I may be able to provide a more helpful answer.

Thanks for the comment.

I need to generate a lot of user profile that have a content type attached to it to store the CCK data such as emp Id, supervisor, etc. What I am really looking for is how to generate content for a user profile programatically. Any help would be greatly appreciated.

Thanks!

You will need to create the related nodes programmatically. I am not sure if you need to programmatically create the users as well as the nodes, or if you already have all the users created, however this may help.

http://codekarate.com/content/creating-organic-groups-group-node-program...

That post shows a little information about creating a node programmatically and you should be able to find other simpler examples on Drupal.org. If you are looking for an example of how the node you are trying to create is currently structured, you can manually create a node, go to that node view page, and put this code in a module.

/**
 * Implements hook_nodeapi().
 */
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'load') {
    print('<pre>'.print_r($node,1).'</pre>');
  }
}

Now if you refresh the page, you should see the entire structure of the node. This can sometimes be helpful when creating the node.

Let me know if this helps.

Thanks for the reply Shane. I have already know how to generate a node programatically. I need to generate the whole content profile problematically. Meaning creating the user + the node attached to that user account together. So the extra information for the user will be saved inside of the cck fields. Below is my code. The problem is that when I create the user programatically. I see inside the site that the user is created with the empty form related to the content node attached to it. But when I use content_profile_load() it returns null. So I am not able to populate the fields of the node. So the node related to that user cannot be found. Do you happen to know what I am doing wrong in my code?

I appreciate your help and advise,

Here is my code:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 
$user_name = 'my_name';
$email = '<a href="mailto:my_name@domain.com" rel="nofollow">my_name@domain.com</a>';
...
 
$fields = array(
   'name' => $user_name,
   'mail' => $email,
   'pass' => $user_password,
   'status' => 1,
 
   );
$account = user_save('', $fields);
 
if ($account != NULL ) {
  echo "user Id: $account->uid";
$profile_node = content_profile_load('bio', $account->uid, true);
 
if ($profile_node != NULL) {
    echo "node Id: $node_ref->nid";
    return array(
        0 => array('nid' => $profile_node->nid ),
        // 0 => array('USER ONE' => $user_data->field_last_name[0]['value']),
    ); 
    //it's a published node, 0 if it's un published
    $profile_node->status = 1;
    $profile_node->title = $account->name;
    //1 is promote to home page, 0 is not to promote on home page
    $profile_node->promote = 0;
    $profile_node->uid = $account->uid;
    $profile_node->format = 1; //filtered HTML
    $profile_node->field_emp_id[0]['value'] = '55555';
    $profile_node->field_first_name[0]['value']= 'user1';
    $profile_node->field_last_name[0]['value'] = 'USER ONE';
    $profile_node->field_title[0]['value']= 'Engineer';
 
    if ($profile_node = node_submit($profile_node)) {
        //saves the node
        node_save($profile_node);
     }
 
else {
    echo " no user data found!";
}
}

I have finally solved the problem by creating and saving the node prior to creating the user and assign it to 'data' field of the user before saving the user. Now I am able to see the bio info populated in the attached node.

$fields = array(
'name' => $user_name,
'mail' => $email,
'pass' => $user_password,
'status' => 1,
'data' => $profile_node,
);

//Create a new user
$account = user_save('', $fields);

Thanks for following up, glad to see you got it figured out. It will actually be useful information for me as well if I ever run into a similar situation.

Thanks.

Hi Shane,

Your article works a treat! I'm using it to create sign up forms embedded in landing pages.

Can you explain how I automatically log in the user after creation, please?

Regards,
Matt.

hi
i want to know one thing actually i am creating a sign up form in drupal 6 in which i want a E-mail field and password field and confirm password field and when user put this information and click on button a email is send to that user where a link is given to him and after click on that link user will redirect to the website page where he/she put there information after submitting it alll value will be save in database can you please tell me how can i do this thing..

thanks
lalit

So I'm trying to make a custom webservice outside of Drupal 6.22 that i can use with a mobile app.

I tried your drupal mail code in a php file, even tried

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

yet neither of those are working, the user isn't being created.

Any help would be appreciated.

Thanks!

Post new comment