Make WordPress Core

Opened 11 years ago

Last modified 6 years ago

#24972 new enhancement

wp_dropdown_roles() multiple pre-selected options

Reported by: pauloasilva's profile PauloASilva Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.6
Component: Users Keywords: has-patch needs-refresh
Focuses: administration, template Cc:

Description

Hi,
I would like to suggest a patch to wp_dropdown_roles() to enable multiple pre-selected options.

Below you'll find the diff and the full featured function.
I'm not sure whether Trac is the right place to put this. If not, please let me know how to procede

diff -r e185f8cbbec5 wp-admin/includes/template.php
--- a/wp-admin/includes/template.php	Tue Aug 06 11:50:50 2013 +0100
+++ b/wp-admin/includes/template.php	Tue Aug 06 19:11:37 2013 +0100
@@ -751,9 +751,19 @@
 /**
  * Print out <option> html elements for role selectors
  *
+ * <code>
+ * // call with a single pre-selected option
+ * wp_dropdown_roles( 'editor' ):
+ *
+ * // call with multiple pre-selected options
+ * wp_dropdown_roles( array( 'editor', 'administrator' ) ):
+ * </code>
+ * 
  * @since 2.1.0
  *
- * @param string $selected slug for the role that should be already selected
+ * @param	string|array	$selected list of role slugs that should be already
+ * selected
+ * @return	string	list of HTML <option> elements with user roles
  */
 function wp_dropdown_roles( $selected = false ) {
 	$p = '';
@@ -761,10 +771,14 @@
 
 	$editable_roles = get_editable_roles();
 
+	// For backwards compatibility
+	if ( is_string($selected) )
+		$selected = array( $selected );
+
 	foreach ( $editable_roles as $role => $details ) {
 		$name = translate_user_role($details['name'] );
-		if ( $selected == $role ) // preselect specified role
-			$p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
+		if ( is_array($selected) AND in_array($role,$selected) ) // preselect specified role
+			$p .= "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
 		else
 			$r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
 	}
/**
 * Print out <option> html elements for role selectors
 *
 * <code>
 * // call with a single pre-selected option
 * wp_dropdown_roles( 'editor' ):
 *
 * // call with multiple pre-selected options
 * wp_dropdown_roles( array( 'editor', 'administrator' ) ):
 * </code>
 * 
 * @since 2.1.0
 *
 * @param	string|array	$selected list of role slugs that should be already
 * selected
 * @return	string	list of HTML <option> elements with user roles
 */
function wp_dropdown_roles( $selected = false ) {
	$p = '';
	$r = '';

	$editable_roles = get_editable_roles();

	// For backwards compatibility
	if ( is_string($selected) )
		$selected = array( $selected );

	foreach ( $editable_roles as $role => $details ) {
		$name = translate_user_role($details['name'] );
		if ( is_array($selected) AND in_array($role,$selected) ) // preselect specified role
			$p .= "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
		else
			$r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
	}
	echo $p . $r;
}

Attachments (5)

wp_dropdown_roles.diff (1.5 KB) - added by PauloASilva 11 years ago.
diff
original-behavior.png (28.8 KB) - added by PauloASilva 7 years ago.
Original behavior
patch-behavior.png (8.9 KB) - added by PauloASilva 7 years ago.
Patch behavior
User-roles.jpg (23.1 KB) - added by joyously 7 years ago.
The Users page has a dropdown role select. This is what it looks like without the patch.
add-new-user.jpg (47.3 KB) - added by joyously 7 years ago.
The Add New User page also uses the dropdown role select. Here it is without the patch.

Download all attachments as: .zip

Change History (21)

#1 follow-up: @sabreuse
11 years ago

Hi, would you mind attaching your diff to the ticket as an attachment so people can download & test it more easily?

#2 in reply to: ↑ 1 @PauloASilva
11 years ago

Hi sabreuse,
Thanks for your reply.

The diff was attached.

Replying to sabreuse:

Hi, would you mind attaching your diff to the ticket as an attachment so people can download & test it more easily?

#3 @nacin
11 years ago

  • Component changed from Template to Users
  • Focuses template added

In order for this to work, wp_dropdown_roles() would need multiple="multiple" as well, right? What's the use case for this?

#4 @PauloASilva
11 years ago

Hi nacin,
wp_dropdown_roles() does not output the <select> part so it has not to deal with it, nevertheless I kept the original behavior.

About the use case, I did suggest this patch when patching a plugin to allow a business rule to be applied to more than one user role.
Because wp_dropdown_roles() is a core function and WP has many user roles, it makes sense to allow multiple items selection.

If you need some other use cases, let me know the best way to provide you them.

Best regards,
Paulo A. Silva

#5 @chriscct7
9 years ago

  • Focuses administration added
  • Keywords has-patch needs-refresh ux-feedback added
  • Severity changed from trivial to normal

This ticket was mentioned in Slack in #design by karmatosed. View the logs.


7 years ago

#7 @melchoyce
7 years ago

If this is still desired, can someone post screenshots or video of what this patch is doing so we can provide UX feedback?

@PauloASilva
7 years ago

Original behavior

@PauloASilva
7 years ago

Patch behavior

#8 @PauloASilva
7 years ago

Hi @melchoyce

The original behavior

https://core.trac.wordpress.org/raw-attachment/ticket/24972/original-behavior.png

and the patched version which allows multiple selection

https://core.trac.wordpress.org/raw-attachment/ticket/24972/patch-behavior.png

Regards,
Paulo A. Silva

@joyously
7 years ago

The Users page has a dropdown role select. This is what it looks like without the patch.

@joyously
7 years ago

The Add New User page also uses the dropdown role select. Here it is without the patch.

#9 @melchoyce
7 years ago

Thanks for screenshots! We'll review this during our next design triage.

#10 @PauloASilva
7 years ago

Let me just add that the default behavior remains the same only the API was extended to optionally handle multiple selection. None of the listed interfaces will be affected by this change.

Plugin developers would take advantage from this whenever they need to collect input about user roles.

This ticket was mentioned in Slack in #design by karmatosed. View the logs.


7 years ago

#12 follow-up: @melchoyce
7 years ago

We talked about this during design office hours today: https://wordpress.slack.com/archives/C02S78ZAL/p1500566770617282

Since there haven't really been any use cases presented for this feature, and default user roles within WordPress increase in privileges, we don't see this being useful unless someone has customized their site's user roles. For that reasons, we think this is plugin territory, rather than core. However, we're happy to hear any counter-points.

Let me just add that the default behavior remains the same only the API was extended to optionally handle multiple selection. None of the listed interfaces will be affected by this change.

Just want to clarify — does this mean WordPress already supports multiple user roles by default on the API side, there's just no supported UI for it yet?

#13 @PauloASilva
7 years ago

Back in 2013, when this ticket was opened, I was working on some plugin whose interface would list user roles, allowing multiple roles to be selected (e.g. to link with post types).

wp_dropdown_roles() is a template function which outputs <option> elements, intended for use with the <select> element, but it does not output it, so a developer can write it as follow to allow multiple entries to be selected at once (e.g. editor and contributor)

<select multiple>
    <?php wp_dropdown_roles( 'editor' ); ?> 
</select>

Then, selected options are persisted on database.
How to do to get persisted options rendered back on this same control?

As is wp_dropdown_roles() does not allow it.
Proposed change makes it not only possible but also seamless, without requiring any custom function implementation or hackish way to do it (e.g. using buffers)

<select multiple>
    <?php wp_dropdown_roles( array( 'editor', 'contributor') ); ?> 
</select>

This way editor and contributor options will be rendered selected.

I think it is interesting for developers.

To clarify the API thing, I was not referring myself to the XML RPC API but to the WordPress core API (functions and Object provided by WordPress core).

Regards,
Paulo A. Silva

#14 in reply to: ↑ 12 @greenshady
7 years ago

Replying to melchoyce:

Since there haven't really been any use cases presented for this feature, and default user roles within WordPress increase in privileges, we don't see this being useful unless someone has customized their site's user roles. For that reasons, we think this is plugin territory, rather than core. However, we're happy to hear any counter-points.

I've got a reasonably popular role management plugin (Members) where this was one of the most-requested features for years. Of course, I added the feature. There are multitudes of use cases where users need to assign multiple roles to a user. bbPress immediately comes to mind, but it handles things gracefully on its own.

I don't believe this is something core should expose as part of the UI and should be left to plugins, particularly when dealing with roles/caps.

I took a different route of handling this in my plugin. Instead of a multiple select, which has problems when dealing with managing multiple users at once on the manage users screen, I removed the core WP select. Then, I replaced it with an "Add Role" dropdown and a "Remove Role" dropdown. I can go into detail about why I went this route instead of a multiple select if need be.

The best course of action would be to provide a filter hook for removing the core select role dropdown. I had to use JavaScript to remove it from the screen. Providing this would make it easier for plugins to hook in and do their own thing.

We could also use a filter hook on the edit and add-new user screens too.

Just want to clarify — does this mean WordPress already supports multiple user roles by default on the API side, there's just no supported UI for it yet?

Yes, it's always supported multiple roles per user since roles were introduced (AFAIK).

This ticket was mentioned in Slack in #design by karmatosed. View the logs.


6 years ago

#16 @karmatosed
6 years ago

  • Keywords ux-feedback removed

As this solution would be a hook it doesn't need ux-feedback from the design team, so removing the tag.

Note: See TracTickets for help on using tickets.