Ref: http://ericlondon.com/programmatically-addupdate-users-using-user-save
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);
_user_mail_notify('status_activated', $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);
?>
_user_mail_notify('status_activated', $account
);
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);
_user_mail_notify('status_activated', $newUserData
);
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);
<?php
// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_form_submit('user_register_form', $form_state);
?>
$newUser = array(
'name' => 'somename',
'pass' => '123',
'profile_name' =>'someprofilename',
'profile_department' =>'somedepartment',
'profile_job_title' =>'someprofilejobtitle',
'mail' => 'somemail',
'access' => 0,
'status' => 1,
'profile_phone' => 'someprofilephone',
'profile_office' => 'someprofileoffice',
);
user_save(null, $newUser);
Here's a way to do it via the forms api:
<?php
// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);
?>
Above taken from: http://api.drupal.org/api/function/drupal_execute/6
Worked for me, only a minor issue, instead of only one password value
the form in drupal 6 requires 2 password values or it fails:
<?php
// this hardcodes the password value; with this you end with user:robo-user, password:password
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
// a better way is to generate a random password and assign it to a variable
$pass = user_password(8);
$form_state['values']['pass']['pass1'] = $pass;
$form_state['values']['pass']['pass2'] = $pass;
?>
- Login to post comments
D7 Example