Defining the Multiple Roles Interface Constant

Multiple Roles Interface contains an optional safeguard to prevent site administrators from accidentally removing all roles from a user during editing. To activate it, all you need to do is add a line defining the constant MULTI_ROLES_EDIT_DEFAULT to your wp-config.php file.

Activating the Safeguard

To enable this safeguard, add this one line to the wp-config.php file in the root of your WordPress installation:

define('MULTI_ROLES_EDIT_DEFAULT', true);

This will activate a conditional check in the function swt_set_multi_roles_edit(), which updates the user roles whenever you edit a user. The function will check if there are any roles assigned. If it finds none, it will automatically assign whatever role is granted when someone registers for the site.

Unless you’ve customized this, the default role will probably be Subscriber. There’s a filter that will let you assign a different role if you choose.

Why the Constant is Necessary

Multiple Roles Interface was originally built for multisite, whose Users pages have a frustrating feature not found on single-site WordPress installations. If a multisite user has no roles or capabilities, that person won’t show up in the Users list, though they are included in the total user count.

If you need to access the user to add a role back, you’ll have to dig around in the database or write a PHP script to restore them.

Defining the Constant on Selected Sites

In multisite you may have just one or two high-usage, high-error-prone sites you want to protect. In this case, don’t define the constant in wp-config.php. Try this instead:

// Safeguard user roles on the sites with IDs of 56 and 87
function define_swt_constant()
{
  global $blog_id;
  if ($blog_id != 56 && $blog_id != 87): 
    return;
  endif;

  if (!defined('MULTI_ROLES_EDIT_DEFAULT')):
    define('MULTI_ROLES_EDIT_DEFAULT', true);
  endif;
}
add_action('admin_init', 'define_swt_constant');

In this code I checked first to see if the constant is already defined. If it is and I try to define it again, it’ll throw errors.

Defining Different Values by Site ID

You can also opt to protect all sites except one or two, which may have a reason for users with no role. Defining the constant as false will have the same effect as leaving it undefined: the safeguard will not be activated.

// Safeguard user roles on all sites but IDs 24 and 109
function define_swt_constant_this_way()
{
  if (!defined('MULTI_ROLES_EDIT_DEFAULT')):
    global $blog_id;
    if ($blog_id == 24 || $blog_id == 109):
      define('MULTI_ROLES_EDIT_DEFAULT', false);
    else:
      define('MULTI_ROLES_EDIT_DEFAULT', true);
    endif;
  endif;
}
add_action('admin_init', 'define_swt_constant_this_way');

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 )

Google+ photo

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

Connecting to %s