Adding users

In a previous post I’ve showed some code to add nodes to Drupal, in this blog I will show that the same can be done with user objects.

$roles = array(
   5 => 'visitor'

$fields = array(
    'name' => $username,
    'mail' => $email,
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => $roles
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);

// $account will contain the new created user object

The fields are rather simple and easy to understand what they do:

  • name; loginname of the user
  • mail; email address
  • pass; plaintext password, will be hashed on save
  • status; 1 for an active user and 0 for an blocked user
  • roles; an array with the roles to assign to the user. The number and name is different per Drupal installation

So it is rather simple to add an user in Drupal, as it should be 🙂

Tagged with: , ,
Posted in Drupal

Comments are closed.