Create New Account


/ Published in: PHP
Save to your folder(s)

Create new user account programmatically - drupal 7


Copy this code and paste it in your HTML
  1. /**
  2.  *
  3.  * Criar nova conta
  4.  *
  5.  * @param $email
  6.  * @return bool
  7.  */
  8. function _create_user_account($email) {
  9. //This will generate a random password, you could set your own here
  10. $password = user_password(8);
  11.  
  12. //set up the user fields. In this case username = mail.
  13. $fields = array(
  14. 'name' => $email,
  15. 'mail' => $email,
  16. 'pass' => $password,
  17. 'status' => 1,
  18. 'init' => 'email address',
  19. 'roles' => array(
  20. DRUPAL_AUTHENTICATED_RID => 'authenticated user',
  21. ),
  22. );
  23.  
  24. //the first parameter is left blank so a new user is created
  25. $account = user_save('', $fields);
  26.  
  27. if(!empty($account)) {
  28. // Manually set the password so it appears in the e-mail.
  29. $account->password = $fields['pass'];
  30.  
  31. // Send the e-mail through the user module.
  32. drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', '[email protected]'));
  33.  
  34. return TRUE;
  35. }
  36. else {
  37. return FALSE;
  38. }
  39. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.