Add and revoke role to / from a user

Public

This function allows to easily add / revoke role from a user / bulk of users:

Get raw version
php
  1. // User ID of user that you want to add role to.
  2. $uid = 5;
  3.  
  4. // Role name to add.
  5. $role_name = 'Member';
  6.  
  7. // Add role.
  8. if ($role = user_role_load_by_name($role_name)) {
  9. user_multiple_role_edit(array($uid), 'add_role', $role->rid);
  10. }
  11.  
  12. // Revoke role.
  13. if ($role = user_role_load_by_name($role_name)) {
  14. user_multiple_role_edit(array($uid), 'remove_role', $role->rid);
  15. }