WordPress.org

Make WordPress Core

Opened 19 months ago

Last modified 13 months ago

#24972 new enhancement

wp_dropdown_roles() multiple pre-selected options

Reported by: PauloASilva Owned by:
Milestone: Awaiting Review Priority: normal
Severity: trivial Version: 3.6
Component: Users Keywords:
Focuses: 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 (1)

wp_dropdown_roles.diff (1.5 KB) - added by PauloASilva 19 months ago.
diff

Download all attachments as: .zip

Change History (5)

comment:1 follow-up: @sabreuse19 months ago

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

@PauloASilva19 months ago

diff

comment:2 in reply to: ↑ 1 @PauloASilva19 months 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?

comment:3 @nacin13 months 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?

comment:4 @PauloASilva13 months 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

Note: See TracTickets for help on using tickets.