Create Drupal User programatically (with profile field values)

Create Drupal User programatically (with profile field values)

15 December 2009 by jay boodhun Create Drupal User programatically (with profile field values) 8 Comments

While i was working on a project recently, i needed to create user accounts programatically through my own module, save some profile fields information and also assign a role to these new users.

After studying the User Module Hook and the Profile Module functions i got this working with the code below:

  

 
<?php
/**
 * Date dd/mm/yyyy
 * Creates a user with profile values.
 *
 * @param $data
 *   $_POST data for example.
 *
 * @return
 *   $user Object.
 */
function my_module_create_user($data) {
	// Drupal User module function that generates MD5 hash password
	$pass = user_password();
	$newuser = array(
	  'name' => $data['name'],
	  'mail' => $data['mail'],
	  'status' => 1, // Sets user as Active
	  'pass' => $pass
	);
	$auto_user = user_save('', $newuser);
	$rid = 4; // Assign Role ID
	$uid = $auto_user->uid;
	db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, $rid);
    // Populates profile values
	$profile = array(
	  'firstname' => $data['firstname'],
	  'lastname' => $data['lastname'],
	  'company' => $data['company'],
	  'address' => $data['address'],
	  'city' => $data['city'],
	  'state' => $data['state'],
	  'country' => $data['country'],
	  'phone' => $data['phone'],
	 );
	 // Adds details under Personal Information Category set in Profile
	 profile_save_profile($profile, $auto_user, 'Personal Information'); 
	 // Load the new user Object
	 $account = user_load(array('uid' => $auto_user->uid, 'status' => 1));
	 global $user;
	 $user = $account;
	 drupal_set_message(t('You have been authenticated'));
	 db_query("UPDATE {users} SET login = %d WHERE uid = %d", time(), $user->uid);
	 sess_regenerate();
	 // Finally send an Email
	 /*
	 * Mail templates available:
	 * register_no_approval_required
	 * register_admin_created
	 * register_pending_approval - Need to set Status above as 0 if this option is required
	 */
	 drupal_mail('user', 'register_no_approval_required', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
          // Set the redirection to where you want
	 drupal_goto('home');
}
?>

 

Figured out the Roles can be added inside the $newuser array thus:


$newuser = array(
            'name' => $data['name'],
            'mail' => $data['mail'],
            'status' => 1, // Sets user as Active
            'pass' => $pass,
             $roles => array('authenticated user', 'some other role'),
           );

instead of doing it separately

Many thanks that is exactly what I was trying to do

Thank you, I was looking for that. This is perfect, includes email, authentication, destination... COOL. Thanks.

It is unclear how it works ?
Create a user ?
Where?
How it works?
Look like nothing happened after run your code.
How to create profile after registration form is submitted ?

This can be done easily with the rules module as well.
@Mark. The profile variable stores the values to create the profile. I am running this inside my own custom module. But Try Rules Module. Much more easier

Thanks a lot for this. But do I have to include any files in this script? I'm including "./includes/bootstrap.inc" but it can't recognize some of the functions like user_password().

If you trying this outside of a module then you have to include this:

require_once('./includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $theme_key, $custom_theme;
$custom_theme = variable_get('theme_default', 'main');
init_theme();

Minor correction on adding roles inside the $newuser array:

$newuser = array(
'name' => $data['name'],
'mail' => $data['mail'],
'status' => 1, // Sets user as Active
'pass' => $pass,
'roles' => array('authenticated user', 'some other role'),
);

Great tip! Thanks!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Syntax highlight code surrounded by the {syntaxhighlighter OPTIONS}...{/syntaxhighlighter} tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.