Drupal

Programmatically create users in Drupal 6.x

by Matt Danger on Dec.16, 2009, under Drupal

You can create a Drupal user by sending user_save() a null parameter and an array of user information. You can also assign a role by setting an array that is keyed with the role ID (rid). This can be obtained by calling user_roles() as shown in the example below.

// Get an array of roles
$roles = user_roles();
 
// Basic account information
$user = array(
  'name' => 'Some User',
  'pass' => 'some_password',
  'mail' => 'user@example.com',
  'status' => 1,
  'init' => 'user@example.com',
  'roles' => array(array_search('some_role', $roles) => 1),
);
 
// See if the user exists by calling Drupal's user_load()
$existing_user = user_load(array('name' => $user['name']));
 
if (!$existing_user->uid) {
 
  // Save the user
  $user = user_save(NULL, $user);
 
}
2 Comments :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Send me a message on AIM!

Friends

Read what I read...