Using WordPress ‘get_role_list’ PHP filter

The get_role_list WordPress PHP filter allows you to modify the array of translated role names for a specific user.

Usage

add_filter('get_role_list', 'your_custom_function', 10, 2);

function your_custom_function($role_list, $user_object) {
    // your custom code here
    return $role_list;
}

Parameters

  • $role_list (array) – An array of translated user role names keyed by role.
  • $user_object (WP_User) – A WP_User object representing the user.

More information

See WordPress Developer Resources: get_role_list

Examples

Add a custom role label

Add a custom role label to the role list for a user with a specific email address.

add_filter('get_role_list', 'add_custom_role_label', 10, 2);

function add_custom_role_label($role_list, $user_object) {
    if ($user_object->user_email == '[email protected]') {
        $role_list['custom_role'] = __('Custom Role', 'textdomain');
    }
    return $role_list;
}

Remove a role label

Remove the ‘Subscriber’ role label from the role list.

add_filter('get_role_list', 'remove_subscriber_label', 10, 2);

function remove_subscriber_label($role_list, $user_object) {
    unset($role_list['subscriber']);
    return $role_list;
}

Modify role label

Change the ‘Editor’ role label to ‘Content Manager’.

add_filter('get_role_list', 'change_editor_label', 10, 2);

function change_editor_label($role_list, $user_object) {
    $role_list['editor'] = __('Content Manager', 'textdomain');
    return $role_list;
}

Sort role labels alphabetically

Sort the role list alphabetically by their labels.

add_filter('get_role_list', 'sort_role_labels_alphabetically', 10, 2);

function sort_role_labels_alphabetically($role_list, $user_object) {
    asort($role_list);
    return $role_list;
}

Add a prefix to role labels

Add a prefix to all role labels.

add_filter('get_role_list', 'add_role_label_prefix', 10, 2);

function add_role_label_prefix($role_list, $user_object) {
    foreach ($role_list as $key => $value) {
        $role_list[$key] = 'Prefix - ' . $value;
    }
    return $role_list;
}