Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#22409 closed enhancement (wontfix)

add multiple selected options support to selected() function

Reported by: bitacre Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.4.2
Component: Template Keywords:
Focuses: Cc:


The current selected() function returns selected="selected" if two strings match, but does not correctly handle arrays that result from select options that have multiple="multiple" enabled (it just checks the first item in the array by forcing a string type).

I suggest either adding correct handling of arrays or alternatively adding a separate multiselected() function to handle this situation.

This could be accomplished by altering the current __checked_selected_helper() function to this:

function __checked_selected_helper( $helper, $current, $echo, $type ) {
  if( is_array( $helper ) && in_array( $current, $helper, true ) )
    $result = " $type='$type'";
  elseif ( (string) $helper === (string) $current )
    $result = " $type='$type'";
    $result = '';
  if ( $echo )
    echo $result;

  return $result;

I've just added the first 2 lines and changed the first if to an elseif.

Change History (3)

#1 @bitacre
7 years ago

This isn't the most efficient way to do this, but I just wanted to demonstrate the point that in_array() should be used instead of === when the $helper is an array.

Last edited 7 years ago by bitacre (previous) (diff)

#2 @nacin
6 years ago

  • Component changed from Validation to Template

#3 @nacin
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

This doesn't seem like something we need to support, as it complicates the function and how it would be used. It would be more intuitive to roll your own handing. Sorry for having not received a reply in so long.

Note: See TracTickets for help on using tickets.