Here's a quick code snippet I use to programmatically create new users:
<?php
$newUser = array(
'name' => 'username',
'pass' => 'password', // note: do not md5 the password
'mail' => 'email address',
'status' => 1,
'init' => 'email address'
);
user_save(null, $newUser);
?>And, here's how you can update an existing user:
<?php
// load user object
$existingUser = user_load('USERID');
// update some user property
$existingUser->some_property = 'blah';
// save existing user
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);
?>If you wanted to update an existing user's profile data:
<?php
// load user object
$existingUser = user_load('USERID');
// create an array of properties to update
$edit = array(
'profile_first_name' => 'Eric'
);
// save existing user
user_save(
(object) array('uid' => $existingUser->uid),
$edit,
'Personal Information' // category
);
?>










Drupal 7, add user
I just added a user in Drupal 7 using this code, and had no issues...
<?php$account = new StdClass();
$account->is_new = TRUE;
$account->status = TRUE;
$account->name = 'someusername';
$account->pass = 'somepassword';
$account->mail = 'email@example.com';
$account->init = 'email@example.com';
$account->roles[5] = 'some role';
$account->field_first_name[LANGUAGE_NONE][0]['value'] = 'some first name';
$account->field_last_name[LANGUAGE_NONE][0]['value'] = 'some last name';
user_save($account);
?>
password not hashed for new accounts
you need to hash the password first
<?php
$account->pass =user_hash_password('somepassword');
?>
if you are updating existing accounts the password gets hashed for you by user_save
guy_schneerson
www.blue-bag.com
Good Post
This is an excellent post, explained complex stuff in a simple manner
Welcome to your new Drupal website!
Hello,
First, I want to thank you for your explanations.
I met a little problem. After adding users programatically, the user log in to the site and see the "Welcome to your new drupal website!" message.
Have I missed something in that array ?
$edit = array(
'mail' => $data['WEMAIL'],
'pass' => $data['ID_PWD'],
'language' => $data['WLG'],
'status' => $status,
'role' => $roles
);
user_save((object) array('uid' => $existingUser->uid), (array) $edit);
Thanks for your answer.
A little bit more advanced example
Some code adjustments and additional properties defining :
// New user creation
$account = new stdClass();
$account->is_new = true;
$newUserData = array(
'name' => 'test user',
'pass' => '123456', // note: do not md5 the password
'mail' => 'email1@email.com',
'status' => 0,
'timezone' => 'Europe/Paris',
'init' => 'email1@email.com',
'roles' => array(
'1' => 'your role 1',
'2' => 'your role 2',
),
);
$new_user = user_save($account, $newUserData);
Thanks, this worked for
Thanks, this worked for me.
To answer a previous comment's question: to send the user a registration email use the drupal function _user_mail_notify().
An example might be:
$newUser = array(
'name' => 'username',
'pass' => 'password', // note: do not md5 the password
'mail' => 'email address',
'status' => 1,
'init' => 'email address'
);
$user = user_save(null, $newUser);
_user_mail_notify('status_activated', $user);
Thanks
This was helpful. Thank you.
How to send user "welcome to xyz site" email on user creation
Hi,
Thanks for the insightful drupal how-to.
Do you know how I would send the email welcoming the person to our site?
e.g. Welcome !name, please click this link to log into the site..."
Any insight would be much appreciated.
Thanks.
@nelsonic
Thank you for this!
This was just what I needed. Thank you and thank you again :)
Thanks. You saved me a trip
Thanks. You saved me a trip to documentation :)
And good thing you didn't go to the docs...
I've been all over the web today, including api.d.o and nowhere did I find node_save() instructions using this format:
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);This is the only code I could get to deal with user updates, so I'm sticking with it!
Thanks, Eric!
Cheers,
Richard aka siliconmeadow
http://drupal.org/user/55284