Programatically create a new role I am trying to create a module which will easily allow me to set up basic sites with specific settings on the fly for a project I'm working on. I am having trouble creating a new user role. I have tried the following code within hook_update_N(), but it doesn't seem to work.

$role = new stdClass(); // create your Class
$role-&gt;name = 'editor';
user_role_save($role);


How do I programatically create a new role?
$roles = array('role1','role2','role3');
foreach($roles as $name) {
 $existing_role = user_role_load_by_name($name);
 if (empty($existing_role)) {
  $role = new stdClass();
  $role-&gt;name = $name;
  user_role_save($role);
 }
}


I have placed the same snippet in module install file and it is working just fine. You need to enable and then uninstall the module and re-enable it, I hope it will work.