[wp-trac] [WordPress Trac] #24972: wp_dropdown_roles() multiple pre-selected options

WordPress Trac noreply at wordpress.org
Tue Aug 6 18:24:31 UTC 2013


#24972: wp_dropdown_roles() multiple pre-selected options
-------------------------+-----------------------------
 Reporter:  PauloASilva  |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Template     |    Version:  3.6
 Severity:  trivial      |   Keywords:
-------------------------+-----------------------------
 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;
 }
 }}}

--
Ticket URL: <http://core.trac.wordpress.org/ticket/24972>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list