Support » Plugin: User Role Editor » Hide Editor Role from drop down in Add New User /User List

  • Resolved Jenny

    (@enigma666666)


    Hi there,

    I have 3 membership levels, apart from Admin.
    The editor is the website admin and he can add, edit and delete users.
    The second role has the same capability of adding, editing and deleting users.

    However, if possible I want the second role only to be able to add a new user with a capability lower than himself.

    Additionally, I do not want the editor or the 2nd user role, to have the option to select editor role as a role for the new user, from the drop down menu in user profile.

    So basically I need to completely remove the editor user role from the selection drop down box (just like administrator is hidden currently). I have already disabled “promote user” for editor role and 2nd user role, so in users list view, they can’t change the user role to editor using bulk actions, but when they add a new user or edit an existing user profile, the editor role appears as an option to select in the drop down on the user’s actual profile page in wp-admin, and this I can’t have.

    If someone can please help me out with a piece of code that I can drop into functions or somewhere, that will resolve this issue?

    https://wordpress.org/plugins/user-role-editor/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi,

    Insert this code the active theme functions.php file:

    add_filter('editable_roles', array($this, 'exclude_editor_role' ) );
    function exclude_editor_role($roles) {
        if (current_user_can('administrator') {
           return;
        }
        if (isset($roles['editor'])) {
          unset($roles['editor']);
        }
    
        return $roles;
    }

    In case you will need user interface to manage role access to the other roles Pro version includes “Other roles access” add-on:
    https://www.role-editor.com/other-roles-access/

    Thread Starter Jenny

    (@enigma666666)

    Hi there,
    Thank you for the code, however there is a syntax error in the code:
    Parse error: syntax error, unexpected ‘{‘

    Your Other roles access add-on, is just what I need, so will purchase that as well, thank you.

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi Jenny,
    Thanks. I edited code online and skipped closing bracket. Fixed variant:

    add_filter('editable_roles', array($this, 'exclude_editor_role' ) );
    function exclude_editor_role($roles) {
        if (current_user_can('administrator')) {
           return;
        }
        if (isset($roles['editor'])) {
          unset($roles['editor']);
        }
    
        return $roles;
    }

    Thread Starter Jenny

    (@enigma666666)

    Thank you for the amended code. Unfortunately now the following error appears in user list page:
    Warning: array_keys() expects parameter 1 to be array, null given in /home/mywebsite/public_html/wp-admin/includes/class-wp-users-list-table.php on line 307

    Also now the ability to change or assign user roles from drop down in user list and also on user profile, has been completely disabled, even for admin, which is not exactly what I wanted.

    What I am trying to achieve, is:
    Hide editor role from appearing as a selection option, in the drop down list on user list page and also on wp add/edit profile page. Only the other available user roles, must appear in the drop down list.

    I think maybe your “other roles access” addon will solve the issue. Can you confirm that your addon will remove editor role from appearing in drop down selection on user list pages as well as on add new user and edit user profile pages in wp admin and that all other user roles, will still appear as option in the drop down lists?

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi Jenny,

    Yes, I confirm that ‘Other roles access’ add-on to the Pro version provides the options and functionality described by you.

    By the way, this is the fully tested version of the code:

    add_filter('editable_roles', 'exclude_editor_role');
    function exclude_editor_role($roles) {
        if (current_user_can('administrator')) {
           return $roles;
        }
        if (isset($roles['editor'])) {
          unset($roles['editor']);
        }
    
        return $roles;
    }

    Thread Starter Jenny

    (@enigma666666)

    Brilliant thank you, much appreciated, the amended code works perfectly!
    I will also purchase the addon as its exactly what I need.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide Editor Role from drop down in Add New User /User List’ is closed to new replies.