Make WordPress Core

Changeset 32484


Ignore:
Timestamp:
05/11/2015 01:10:19 PM (11 years ago)
Author:
boonebgorges
Message:

In category dropdown, 'selected' should match against 'value_field'.

Props tlexcellent.
Fixes #32330.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r32292 r32484  
    11661166                $cat_name = apply_filters( 'list_cats', $category->name, $category );
    11671167
    1168                 if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) {
    1169                         $args['value_field'] = 'term_id';
    1170                 }
    1171 
    1172                 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\"";
    1173 
    1174                 if ( $category->term_id == $args['selected'] )
     1168                if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
     1169                        $value_field = $args['value_field'];
     1170                } else {
     1171                        $value_field = 'term_id';
     1172                }
     1173
     1174                $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
     1175
     1176                if ( $category->{$value_field} == $args['selected'] )
    11751177                        $output .= ' selected="selected"';
    11761178                $output .= '>';
  • trunk/tests/phpunit/tests/category.php

    r31622 r32484  
    328328                $this->assertContains( 'value="' . $cat_id . '"', $found );
    329329        }
     330
     331        /**
     332         * @ticket 32330
     333         */
     334        public function test_wp_dropdown_categories_selected_should_respect_custom_value_field() {
     335                $c1 = $this->factory->category->create( array(
     336                        'name' => 'Test Category 1',
     337                        'slug' => 'test_category_1',
     338                ) );
     339
     340                $c2 = $this->factory->category->create( array(
     341                        'name' => 'Test Category 2',
     342                        'slug' => 'test_category_2',
     343                ) );
     344
     345                $found = wp_dropdown_categories( array(
     346                        'echo' => 0,
     347                        'hide_empty' => 0,
     348                        'value_field' => 'slug',
     349                        'selected' => 'test_category_2',
     350                ) );
     351
     352                $this->assertContains( "value=\"test_category_2\" selected=\"selected\"", $found );
     353        }
    330354}
Note: See TracChangeset for help on using the changeset viewer.