Ticket #17924 (new enhancement)

Opened 11 months ago

Last modified 3 weeks ago

Make Integrating Multiple Roles Per User Easier for Plugin Authors

Reported by: mobius5150 Owned by:
Priority: normal Milestone: Awaiting Review
Component: Role/Capability Version: 3.2
Severity: normal Keywords: has-patch
Cc: Mobius5150, anointed, dragunoff, NateJacobs, pauli.price@…, deltafactory, sascha@…, knut@…

Description

WordPress supports multiple role per user, however to use this feature is very difficult for plugin authors to integrate well, and often requires adding plugin admin pages instead of integrating with the pre-existing user-edit.php. Due to the difficulty of this, and the fact that most plugins that allow this aren't very user-friendly, I believe users are staying away from multiple roles.

This ticket aims to change how easy it is to work with the current role management system, as well as to add a couple role-based filters and actions, while still keeping the front-end of the administrative interface single-role only. (Because it seems to be the prevailing opinion the multiple roles per user is plugin territory.) In addition, I completely agree that the vast majority of sites can best function with one role per user, however in some cases (such as the site I'm working on now) you simply need to be able to choose multiple roles.

Changes

First Patch File

attachment:Replace_Admin_Role_Dropdown.patch Download The first patch file moves the code for the role dropdown off of /wp-admin/user-edit.php, /wp-admin/includesclass-wp-users-list-table.php, /wp-admin/users-new.php, /wp-admin/options-general.php and into /wp-admin/includes/template.php where two new functions are created:

wp_get_dropdown_roles()

This function does what wp_dropdown_roles used to do, but it returns the value instead of echoing it. (Note that wp_dropdown_roles() now acts as a wrapper for wp_get_dropdown_roles(). (The old function is still in place for backwards compatibility.)

wp_user_role_selector()

This function creates the full dropdown and applies a new 'user_role_selector' filter to the markup before echoing it out. This way the default dropdown can be overridden by something else (perhaps checkboxes).

Second Patch File

attachment:Create_Role_Update_Actions_Filters.patch Download The second patch file changes the functions that handle user creation and updating so they can be hooked into and a developer could make this work with multiple roles. This file affects /wp-admin/includes/users.php and /wp-includes/user.php.

wp_sanitize_user_role()

A new sanitize_user_role filter was created and sanitation of $_POST['role'] in /wp-admin/includes/user.php when user-edit.php is submitted was moved there. The new function is called wp_sanitize_user_role(). (This is so that if user-edit.php is submitted and $_POST['role'] were to contain a value other than a string (perhaps an array of roles) it can be sanitized and worked with accordingly.

pre_user_role

A new pre_user_role filter was added to wp_insert_user() in /wp-includes/user.php. This was mainly done because almost every other field that this function processes has a matching filter like this, and I thought maybe $roles felt left out as it did not. :(

I supposed somebody could also use it for something useful as well.

apply_user_role

A new apply_user_role action was added to wp_insert_user(). The function itself no longer uses $user->set_role(), instead saving user role changes is now handled by this new action. A matching function wp_apply_user_role() was added to set the user role like wp_insert_user() used to do. This way however, a plugin author could simply hook into apply_user_role to apply multiple roles to a user.

Conclusion

All in all, this patch was designed so that the average user would never even notice that a change has been made to user management, however a plugin author looking to add an easy way for people to select multiple roles for a user can quickly hook into this and have a nice, easy, well integrated and WordPress-like plugin.

I have done a few tests adding users and changing roles around with these patches applied on a fresh installation with no plugins and the only changes to WordPress being those that are in the attached patches, and everything seems to work seamlessly.

By the way, I am quite new to WordPress, so I added the needs-testing tag, because it does, but I also want to make sure I've done everything the 'WordPress' way and I want to make sure I havn't missed anything that is broken because of these small changes.

Attachments

Replace_Admin_Role_Dropdown.patch Download (9.3 KB) - added by mobius5150 11 months ago.
The first patch.
Create_Role_Update_Actions_Filters.patch Download (5.0 KB) - added by mobius5150 11 months ago.
The second patch.
Create_Role_Update_Actions_Filters_3.2.1.patch Download (4.8 KB) - added by mobius5150 11 months ago.
The second patch.
Replace_Admin_Role_Dropdown_3.2.1.patch Download (9.3 KB) - added by mobius5150 11 months ago.
The first patch.

Change History

comment:1 follow-up: ↓ 3   scribu11 months ago

Related: #10201

  • Cc anointed added

comment:3 in reply to: ↑ 1   mobius515011 months ago

I updated the second diff so that the users list table on /wp-admin/users.php now uses a user_role_name filter to list the roles of the user. In addition, if the $role variable that $WP_Users_List_Table->single_row() is left empty, it will load an array of roles from the $user_object passed to it.

Again, this has not been updated to actually use multiple roles, it simply provides of means for plugin authors to work with this table.

Replying to scribu:

Related: #10201

Yes, the comment here by Jeremy Clarke presented good points and is what initially got me thinking about this. I just felt that the initial direction of that ticket was pretty different than what I had in mind.

Updated the filter in the first patch to pass the args of the wp_user_role_selector() function to the filter.

The first patch.

The second patch.

I've uploaded a new copy of each patch with 3.2.1 in the file name -- these have been modified to work with the new release of 3.2.1

Really hoping this will gain some traction, I'd really like to see this ability in WordPress, and I have several projects that would highly benefit from this.

The second patch.

The first patch.

I could get behind this.

This would be really useful. I'd love to have multiple roles per user.

I'd use it to define a number of roles, each with a separate group of capabilities: for example, one around creating and managing blog posts, one around each type of custom post, one for moderating forums (I'm using buddypress) and so on. And then a user could be a blog manager, a forums moderator, or both. At the moment you have to define a separate role for each possible combination of responsibilities.

  • Cc dragunoff added
  • Cc NateJacobs added
  • Cc pauli.price@… added
  • Cc deltafactory added
  • Cc sascha@… added
  • Keywords dev-feedback needs-testing removed

Hi Everyone,

Sorry I have not had time to generate a diff patch for this as things have been changing on my end.

I have however updated the code to work with the latest version of WordPress at the time of writing. It is available on my blog at:  http://michaelblouin.ca/2011/multiple-roles-per-user-in-wordpress

Just scroll to the bottom of the article and follow the instructions to install both the modified version of WP and te plugin and you'll have multiple roles per user.

In the meantime if anyone would be willing to petition anyone higher up on the commit chain to support our cause it would be highly appreciated.

If we can get more support I would release the neccesary diff to apply this to the latest WP version

Best regards, Michael Blouin (Mobius5150)

comment:14 follow-up: ↓ 15   greenshady8 weeks ago

Just wanted to chime in here to say that multiple roles per user is one of the most-requested features for my Members plugin. I'm definitely on board with testing this and trying to get it into 3.5.

comment:15 in reply to: ↑ 14   mobius51506 weeks ago

Replying to greenshady:

Just wanted to chime in here to say that multiple roles per user is one of the most-requested features for my Members plugin. I'm definitely on board with testing this and trying to get it into 3.5.

This is great news. I'm just entering exam weeks here, so it will be a little while before I resurface, but expect me to be fully back at it with this by (very) early may at the latest to make a push for 3.5.

  • Cc knut@… added

Definitely added.

I too am willing to test this as it is a feature that is most often requested by my clients.

Note: See TracTickets for help on using tickets.