Last active
October 11, 2015 08:07
-
-
Save ss81/3828364 to your computer and use it in GitHub Desktop.
Create a user account in Drupal 7 programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//This will generate a random password, you could set your own here | |
$password = user_password(8); | |
//set up the user fields | |
$fields = array( | |
'name' => 'user_name', | |
'mail' => '[email protected]', | |
'pass' => $password, | |
'status' => 1, | |
'init' => 'email address', | |
'roles' => array( | |
DRUPAL_AUTHENTICATED_RID => 'authenticated user', | |
), | |
); | |
$fields['field_first_name'][LANGUAGE_NONE][0]['value'] = 'First Name'; | |
$fields['field_last_name'][LANGUAGE_NONE][0]['value'] = 'Last Name'; | |
//the first parameter is left blank so a new user is created | |
$account = user_save('', $fields); | |
// If you want to send the welcome email, use the following code | |
// Manually set the password so it appears in the e-mail. | |
$account->password = $fields['pass']; | |
// Send the e-mail through the user module. | |
drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]')); | |
// (c) http://codekarate.com/blog/create-user-account-drupal-7-programmatically |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment