Enable Multiple User Roles in WordPress for Better Permissions Management
In WordPress, users are typically assigned a single role, such as Administrator, Editor, or Subscriber. However there are situations where a user might need multiple roles. For example, you might want a user to have the permissions of both an Editor and a Contributor, or a Shop Manager and an Author for a more complex content management system.
Add the Ability to Select Multiple Roles in the User Profile:
We’ll begin by adding a multi-select drop-down to the user profile page in the WordPress admin dashboard, where administrators can select multiple roles for a user.
Code to Add Roles Selection Field:
function allow_multiple_roles($user){
if(current_user_can('edit_users')) {
$all_roles = wp_roles()->roles; //Get all available roles
$user_roles = $user->roles; //Get current user's role
?>
<h3><?php _e('Multiple Roles', your_text_domain); ?></h3>
<table class="form-table">
<tr>
<th><label for="multiple_roles"><?php _e('Assign Multiple Roles', your_text_domain); ?></label></th>
<td>
<select name="multiple_roles[]" id="multiple_roles" multiple="multiple" class="regular-text" style="width: 200px;">
<?php foreach ($all_roles as $role_slug => $role_details): ?>
<option value="<?php echo esc_attr($role_slug) ?>" <?php echo in_array($role_slug, $user_roles) ? 'selected':''; ?>>
<?php echo esc_html($role_details['name']); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Select multiple roles for this user.', your_text_domain); ?></p>
</td>
</tr>
</table>
<?php
}
}
//Add the field to the user profile pages
add_action('show_user_profile', 'allow_multiple_roles');
add_action('edit_user_profile', 'allow_multiple_roles');
The allow_multiple_roles()
function adds a multi-select dropdown field on the user profile page.
The dropdown is populated with all available roles in WordPress (wp_roles()->roles).
If the user already has a particular role, it will be pre-selected in the dropdown.
Save the Selected Roles:
This part of the code will save the selected roles when the user profile is updated.
function save_multiple_roles($user_id) {
// Make sure the current user has permission to edit this user
if(!current_user_can('edit_user', $user_id)) {
return false;
}
//Check if the 'multiple_roles' field is set and is an array
if(isset($_POST['multiple_roles']) && is_array($_POST['multiple_roles'])) {
$roles = $_POST['multiple_roles'];
//Get the user object
$user = new WP_User($user_id);
//Remove all current roles
$user->set_role('');
//Add selected roles
foreach ($roles as $role) {
//Validate the role before adding
if(array_key_exists($role, wp_roles()->roles)) {
$user->add_role($role);
}
}
}
}
// Hook the saving process to the 'profile update' action
add_action('profile_update', 'save_multiple_roles');
The save_multiple_roles()
function first checks if the user has permission to edit the profile.
It then looks for the multiple_roles
field and processes it as an array.
Before adding the new roles, the user’s existing roles are removed with $user->set_role('')
.
The selected roles are validated then added to the user using $user->add_role($role)
.
After adding the code to your theme’s functions.php
or a custom plugin, you can test it by following steps:
Go to User Profile: Navigate to Users>All Users and click on the “Edit” link of a user.
Select Multiple Roles: In user’s profile page, you’ll now see a multi-select-dropdown labeled “Assign Multiple Roles”. Select the roles you want to assign to the user.
Save Changes: After making the selections, click the “Update User” button.
Check the User’s Roles: To ensure that the roles were assigned correctly, you can revisit the user’s profile and confirm that the roles are displayed in the multi-select field.