6

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.

  • The wp_dropdown_roles() dropdown is populate by get_editable_roles() which then fetches roles from $GLOBALS['wp_roles']->all_roles. That somehow makes me wonder. See what you get in a callback on the editable_roles filter (one arg: Roles array). – kaiser Apr 11 '14 at 0:47
  • So I printed out that $wp_roles->all_roles after my function got executed, and the subscriber role disappeared. However - when I added that filter, and printed out both $wp_roles->all_roles and $all_roles subscriber was back! It's like it's being added back somehow... – Marco Apr 11 '14 at 15:02
  • I guess that's because you have added it as default role in your settings? – kaiser Apr 11 '14 at 15:19
  • I just double checked - and it's not set as the default role for either of the two sites present. Might this have to do with the fact that a theme's functions.php file (where this is included) does not run on a the network admin's user page? – Marco Apr 11 '14 at 15:21
  • Please deactivate all plugins and 3rd party themes. Move your code to a custom plugin that contains nothing else, to get a clean state. Else we won't ever know if there is some interference. – kaiser Apr 11 '14 at 15:24
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.