Skip to content

Instantly share code, notes, and snippets.

@nikolov-tmw
Last active October 5, 2021 03:56
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save nikolov-tmw/7808046 to your computer and use it in GitHub Desktop.
Save nikolov-tmw/7808046 to your computer and use it in GitHub Desktop.
Multiple roles per user WordPress plugin.
<?php
/**
* Plugin Name: Multiple Roles per User
* Description: Allows anyone who can edit users to set multiple roles per user. In a default WordPress environment that wouldn't have much of an effect, since the user would have the privileges of the top-privileged role that you assign to him. But if you have custom roles with custom privileges, this might be helpful.
* Version: 1
* Author: nikolov.tmw
* Author URI: http://paiyakdev.com/
* License: GPL2
*/
/*
Copyright 2013 Nikola Nikolov (email: nikolov.tmw@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function mrpu_plugin_init() {
load_plugin_textdomain( 'multiple-roles-per-user', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'mrpu_plugin_init' );
function mrpu_admin_enqueue_scripts( $handle ) {
if ( 'user-edit.php' == $handle ) {
// We need jQuery to move things around :)
wp_enqueue_script( 'jquery' );
}
}
add_action( 'admin_enqueue_scripts', 'mrpu_admin_enqueue_scripts', 10 );
/**
* Adds the GUI for selecting multiple roles per user
*/
function mrpu_add_multiple_roles_ui( $user ) {
// Not allowed to edit user - bail
if ( ! current_user_can( 'edit_user', $user->ID ) ) {
return;
}
$roles = get_editable_roles();
$user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) ); ?>
<div class="mrpu-roles-container">
<h3><?php _e( 'User Roles', 'multiple-roles-per-user' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="user_credits"><?php _e( 'Roles', 'multiple-roles-per-user' ); ?></label></th>
<td>
<?php foreach ( $roles as $role_id => $role_data ) : ?>
<label for="user_role_<?php echo esc_attr( $role_id ); ?>">
<input type="checkbox" id="user_role_<?php echo esc_attr( $role_id ); ?>" value="<?php echo esc_attr( $role_id ); ?>" name="mrpu_user_roles[]"<?php echo in_array( $role_id, $user_roles ) ? ' checked="checked"' : ''; ?> />
<?php echo $role_data['name']; ?>
</label>
<br />
<?php endforeach; ?>
<br />
<span class="description"><?php _e( 'Select one or more roles for this user.', 'multiple-roles-per-user' ); ?></span>
<?php wp_nonce_field( 'mrpu_set_roles', '_mrpu_roles_nonce' ); ?>
</td>
</tr>
</table>
</div>
<?php
// Do some hacking around to hide the built-in user roles selector
// First hide it with CSS and then get rid of it with jQuery ?>
<style>
label[for="role"],
select#role {
display: none;
}
</style>
<script type="text/javascript">
(function($){
$(document).ready(function(){
var row = $('select#role').closest('tr');
var clone = row.clone();
// clone.insertAfter( $('select#role').closest('tr') );
row.html( $('.mrpu-roles-container tr').html() );
$('.mrpu-roles-container').remove();
})
})(jQuery)
</script>
<?php }
add_action( 'edit_user_profile', 'mrpu_add_multiple_roles_ui', 0 );
/**
* Saves the selected roles for the user
*/
function mrpu_save_multiple_user_roles( $user_id ) {
// Not allowed to edit user - bail
if ( ! current_user_can( 'edit_user', $user_id ) || ! wp_verify_nonce( $_POST['_mrpu_roles_nonce'], 'mrpu_set_roles' ) ) {
return;
}
$user = new WP_User( $user_id );
$roles = get_editable_roles();
$new_roles = isset( $_POST['mrpu_user_roles'] ) ? (array) $_POST['mrpu_user_roles'] : array();
// Get rid of any bogus roles
$new_roles = array_intersect( $new_roles, array_keys( $roles ) );
$roles_to_remove = array();
$user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) );
if ( ! $new_roles ) {
// If there are no roles, delete all of the user's roles
$roles_to_remove = $user_roles;
} else {
$roles_to_remove = array_diff( $user_roles, $new_roles );
}
foreach ( $roles_to_remove as $_role ) {
$user->remove_role( $_role );
}
if ( $new_roles ) {
// Make sure that we don't call $user->add_role() any more than it's necessary
$_new_roles = array_diff( $new_roles, array_intersect( array_values( $user->roles ), array_keys( $roles ) ) );
foreach ( $_new_roles as $_role ) {
$user->add_role( $_role );
}
}
}
add_action( 'edit_user_profile_update', 'mrpu_save_multiple_user_roles' );
/**
* Gets rid of the "Role" column and adds-in the "Roles" column
*/
function mrpu_add_roles_column( $columns ) {
$old_posts = isset( $columns['posts'] ) ? $columns['posts'] : false;
unset( $columns['role'], $columns['posts'] );
$columns['mrpu_roles'] = __( 'Roles', 'multiple-roles-per-user' );
if ( $old_posts ) {
$columns['posts'] = $old_posts;
}
return $columns;
}
add_filter( 'manage_users_columns', 'mrpu_add_roles_column' );
/**
* Displays the roles for a user
*/
function mrpu_display_user_roles( $value, $column_name, $user_id ) {
static $roles;
if ( ! isset( $roles ) ) {
$roles = get_editable_roles();
}
if ( 'mrpu_roles' == $column_name ) {
$user = new WP_User( $user_id );
$user_roles = array();
$_user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) );
foreach ( $_user_roles as $role_id ) {
$user_roles[] = $roles[ $role_id ]['name'];
}
return implode( ', ', $user_roles );
}
return $value;
}
add_filter( 'manage_users_custom_column', 'mrpu_display_user_roles', 10, 3 );
@mathieuthollet
Copy link

Great !!

@scheidler
Copy link

Exactly what I was looking for. This is not in the Wordpress repository?

@evankennedy
Copy link

Check out my fork. I've added it to the "Add User" page, removed jQuery dependency, and moved styles to the head tag.
https://gist.github.com/evankennedy/072fd504446d9e9d9167

@jemoreto
Copy link

Updated here, working on new user creation: https://gist.github.com/jemoreto/5a8be8ca3e9169d37d18
Thanks, guys!!!

@alimoshen
Copy link

Wow mate you are a life saver!! This worked perfect for me.

@ray-ang
Copy link

ray-ang commented Nov 9, 2019

I added a function when adding a user to prevent the in_array() and array_intersect() errors. The checkboxes in the add user page are also long, so had to remove 'class="mrpu-roles-container"'. The fork can be found at https://gist.github.com/ray-ang/3c77e664b73192b5b2d0a7dd65655413

@eversionsystems
Copy link

eversionsystems commented Feb 29, 2020

Doesn't support changing your own roles. I've forked and added this ability. https://gist.github.com/eversionsystems/6f09a3170e64456d404adfc6c1c9b38b

@alimoshen
Copy link

Doesn't support changing your own roles. I've forked and added this ability. https://gist.github.com/eversionsystems/6f09a3170e64456d404adfc6c1c9b38b

Awesome! I'll give this a try

@Optimator
Copy link

Thank you so much!

@marsvieyra
Copy link

I love you

@eversionsystems
Copy link

@saimjohn, place it in a folder in the wp-content\plugins directory or just in the root path of your wp-content\plugins directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment