Create a user account in Drupal 6 programmatically
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 = "example@example.com"; //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', 'noreply@example..com'));
Useful? Let me know what you think.



Discussions
How can i save a date field.
Could you please tell me how can I save a date field? I have the day, month and year of that field. Thank you very much.
I think I can help if I get some more information
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
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!
Creating nodes programmatically
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.
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.
Create a user account in Drupal 6 programmatically
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:
Create a user account in Drupal 6 programmatically
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);
Good to know
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.
Log in the user after creation
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.
This might help you find what you are looking for
You might be able to use the user_external_login function for Drupal 6.
http://api.drupal.org/api/drupal/modules!user!user.module/function/user_external_login/6
Otherwise have a look at the user_authenticate function.
http://api.drupal.org/api/drupal/modules%21user%21user.module/function/u...
One of those two should help you out.
drupal 6 user form
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
Post new comment