Using the Roles List Filter

The Multiple Roles Interface function to display the checklist of roles on the user-edit.php page has a filter hook, swt_list_multi_roles_edit, that’s useful for sorting roles or removing an item not already filtered out elsewhere by WordPress.

Requirements

Whatever functions you attach to this hook, they always need to pass in the array $roles as an argument and return $roles as an array of the same form.

Example: Sorting Roles

PHP has lots of functions for sorting and manipulating arrays. Here’s a simple example of how to organize roles in alphabetical order, which may be useful if you have numerous custom roles:

// Sort the roles for display on edit-user.php
function sort_the_roles($roles) 
{
  asort($roles);
  return $roles;
}
add_filter('swt_list_multi_roles', 'sort_the_roles');

If you’re on multisite and you need to apply the filter to just one site — let’s say it’s the site with the ID number of 3 — and ignore the others. A function such as this will return $roles untouched if it’s not the site in question and will sort $roles if it is.

// Sort roles for blog with ID of 3 only
function sort_the_roles_on_three($roles) 
{
  global $blog_id;
  if ($blog_id != 3): 
    return $roles; 
  endif;
  asort($roles);
  return $roles;
}
add_filter('swt_list_multi_roles', 'sort_the_roles_on_three');

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s