Assign default role to new registered user (Drupal 7)

Public

Assign a role to a newly registered/created user in Drupal 7

Get raw version
php
  1. /**
  2.  * Implementaion of hook_user_presave().
  3.  */
  4. function [hook]_user_presave(&$edit, $account, $category) {
  5. // $edit is the place where user information can be altered,
  6. // so assigning role can be achieved by:
  7. // - index 5 is the role I have assigned
  8. // - value 1 is the indicator for this role being checked
  9.  
  10. if (isset($account->is_new) && !empty($account->is_new)) {
  11. $role = user_role_load_by_name('abonne');
  12. $edit['roles'] = array($role->rid => 1);
  13. }
  14. }