Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/editable roles

This page redirects to an external site: https://developer.wordpress.org/reference/hooks/editable_roles/

Description

editable_roles is a filter applied by the function get_editable_roles() to the list of roles that one user can assign to others (a user must have the edit_users capability to change another user's role). This list is displayed in the bulk operations (if the user has the list_users and promote_users) of the Users Screen, and on the profile screen.

Usage

<code style="color: #000000"><span style="color: #0000BB"><?php
</span><span style="color: #007700">function </span><span style="color: #0000BB">my_editable_roles</span><span style="color: #007700">(</span><span style="color: #0000BB">$all_roles</span><span style="color: #007700">) {
    </span><span style="color: #FF8000"># ...
</span><span style="color: #007700">}
</span><span style="color: #0000BB">add_filter</span><span style="color: #007700">(</span><span style="color: #DD0000">'editable_roles'</span><span style="color: #007700">, </span><span style="color: #DD0000">'my_editable_roles'</span><span style="color: #007700">);</span></code>

Parameters

$all_roles
(array) (required) All roles.
Default: None

Return

Filter functions should return an array of roles with the same format as the $all_roles parameter.

Examples

Filter out roles with levels higher than the current user's:

<?php
function remove_higher_levels($all_roles) {
    $user = wp_get_current_user();
    $next_level = 'level_' . ($user->user_level + 1);

    foreach ( $all_roles as $name => $role ) {
        if (isset($role['capabilities'][$next_level])) {
            unset($all_roles[$name]);
        }
    }

    return $all_roles;
}

add_filter('editable_roles', 'remove_higher_levels');

Add a "No role" option that sets users' roles to nothing on pages other than the user profile screen (where it already exists):

<?php
function add_empty_editable_role($all_roles) {
    $screen = get_current_screen();

    if (! (isset($all_roles['']) || 'user-edit' == $screen->id)) {
        $all_roles[''] = array(
            'name' => __('— No role for this site —'),
            'capabilities' => array(),
            );
    }

    return $all_roles;
}

add_filter('editable_roles', 'add_empty_editable_role');

Change Log

Since: Version 2.8

Source Files

This filter is applied by:

Related

Functions

This page is marked as incomplete. You can help Codex by expanding it.