Posts Tagged user

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

,

Leave a comment