So I’ve been going through hell with Custom Roles. I will try to be as descriptive as possible, to highlight my issue. Before anyone mentions it – I would like to avoid using a plugin, since that, to me, is overkill of what I view to be a simple problem.
What I want:
3 Roles – Admin, Lesser Admin, User
What I originally attempted:
I removed the two roles Author & Contributor, to leave Administrator, Editor, and Subscriber.
That worked no problem. I am having a problem with the next piece – to rename the remaining roles.
I originally just did the most common suggestion
global $wp_roles;
$wp_roles->roles['editor']['name'] = 'Lesser Admin';
$wp_roles->role_names['editor'] = 'Lesser Admin';
for the last two roles (I kept Admin the same because it was close enough). But this didn’t work. No matter what hooks I attached to it (I put it under add_action('init',...)
but that did not work.
So I tried to be fancy and wrote my own code to handle this.
function permissionsModification(){
$blogs = wp_get_sites();
$current_blog = get_current_blog_id();
foreach($blogs as $blog):
switch_to_blog($blog['blog_id']);
if(get_role('author') !== null)
remove_role('author');
if(get_role('contributor') !== null)
remove_role('contributor');
$toChange = array(
'editor' => array('lesser', 'Lesser Admin'),
'subscriber' => array('user', 'User')
);
foreach($toChange as $old => $new):
$role = get_role($old);
if($role !== null):
if(get_role($new[0]) === null)
add_role($new[0], $new[1], $role->capabilities);
$users = get_users(array('role'=>$old));
foreach($users as $user):
$user->remove_role($old);
$user->add_role($new[0]);
endforeach;
$opt = get_option('default_role');
if($opt === $old)
set_option('default_role', 'administrator');
remove_role($old);
endif;
endforeach;
endforeach;
switch_to_blog($current_blog);
}
add_action('init','permissionsModification');
The Good News: It did succeed in renaming the roles, and the old roles were gone.
Bad News: An extremely strange bug is occurring. I am getting “Undefined Index: Name” errors everywhere in Capabilities.php. I searched the database (have phpMyAdmin access), and found that in one of the sites – the subscriber role was still there! I thought it might have something to do with default roles, so since registrations are prohibited I set it to administrator to test, but still no dice (although it did set that to the default role).
Viewing the network admin user panel shows an “empty” space for where a role should be, and if you inspect the element it has the value subscriber. So I am pretty certain that the subscriber role’s name got deleted and that’s where the errors are coming from.
Now, the interesting part – I walked through this code line by line with print_r
riddled everywhere – and the subscriber role is indeed getting deleted. But I have no idea how it’s getting added back in. I wonder if this has to do with the fact that this is a multisite wordpress, since the subscriber role only is present in one table. But I am still a bit lost on how to fix it.
Any advice would be much appreciated.
So, thanks to kaiser, I was able to figure it out. I had to do two things to make this work.
First, I noticed that disabling the WP User Avatar plugin was causing the errors to disappear. I looked and realized that a setting was checked “allow subscribers and contributors to edit their avatars.” Unchecking it and saving it for each site made the errors disappear.
The other thing I had to do, is that the change would not carry through to other sites (even though the errors were gone), so I moved that entire function from the theme to a plugin in the mu-plugins folder. This made it run all the time (and for every site) so all the changes were occurring without error.