Programmatically create users in Drupal 6.x

by Matt Danger on December 16, 2009

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 for this entry:
  1. TED


    Medicamentspot.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. No prescription drugs. Order pills online

    Buy:Benicar.Lipitor.Acomplia.Wellbutrin SR.SleepWell.Seroquel.Buspar.Zetia.Aricept.Nymphomax.Female Pink Viagra.Ventolin.Female Cialis.Lipothin.Prozac.Zocor.Cozaar.Lasix.Amoxicillin.Advair….

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...