Programmatically assign user role I have multiple roles on my website, and I want to assign a certain role to some users when they register to my site.  

How can I assign a specific role to new users programmatically when they submit their registration forms?
I have tried modules like logintoboggan, autoassignrole, but they provide a default user role on registration, when I want to assign role to specific users, not to all users signing up on my site.
We also solved this task with the user_multiple_role_edit() function.
In our usecase we didn't react on the register-form but on a special menu-callback to create users on the fly.

First: Create a user

$user = new stdClass();
$user-&gt;name = $name;
$user-&gt;status = 1;
user_save($user);


Second: Assign the user to the role

$role = user_role_load_by_name("my custom role");
user_multiple_role_edit(array($user-&gt;uid), 'add_role', $role-&gt;rid);