Programmatically create user account in drupal 7


It is the clean and tested code for programmatically create a user account in drupal system. Anyone can use this code according to their requirement and fields.

////// Creating user //////
$password = user_password(8);
$roles = user_roles(TRUE);
$your_rid = array_search(‘YOUR OTHER ROLE’, $roles);
//set up the user fields
$fields = array(
‘name’ => $node->field_mail[‘und’][0][’email’],
‘mail’ => $node->field_mail[‘und’][0][’email’],
‘pass’ => $password,
‘status’ => 1,
‘roles’ => array(
DRUPAL_AUTHENTICATED_RID => ‘authenticated user’,
$your_rid => ‘YOUR OTHER ROLE’,
),
‘field_first_name’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_first_name[‘und’][0][‘value’]
),
),
),
‘field_last_name’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_last_name[‘und’][0][‘value’],
),
),
),
‘field_telephone’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_telephone[‘und’][0][‘value’],
),
),
),
);
// First parameter is left blank so a new user is created.
$account = user_save(NULL, $fields);

Advertisements

,

  1. Leave a comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: