Programmatically assign user role in Drupal 7
You can use the following code for the adding roles programmatically in Drupal 7.
$role = user_role_load_by_name("my custom role"); user_multiple_role_edit(array($user->uid), 'add_role', $role->rid);