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

Props tlexcellent.
Fixes #32330.

git-svn-id: https://develop.svn.wordpress.org/trunk@32484 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-05-11 13:10:19 +00:00
parent 39639c79da
commit 1a8bd3bf07
2 changed files with 30 additions and 4 deletions

View File

@ -1165,13 +1165,15 @@ class Walker_CategoryDropdown extends Walker {
/** This filter is documented in wp-includes/category-template.php */ /** This filter is documented in wp-includes/category-template.php */
$cat_name = apply_filters( 'list_cats', $category->name, $category ); $cat_name = apply_filters( 'list_cats', $category->name, $category );
if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) { if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
$args['value_field'] = 'term_id'; $value_field = $args['value_field'];
} else {
$value_field = 'term_id';
} }
$output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\""; $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
if ( $category->term_id == $args['selected'] ) if ( $category->{$value_field} == $args['selected'] )
$output .= ' selected="selected"'; $output .= ' selected="selected"';
$output .= '>'; $output .= '>';
$output .= $pad.$cat_name; $output .= $pad.$cat_name;

View File

@ -327,4 +327,28 @@ class Tests_Category extends WP_UnitTestCase {
// Test to see if it returns the default with the category slug. // Test to see if it returns the default with the category slug.
$this->assertContains( 'value="' . $cat_id . '"', $found ); $this->assertContains( 'value="' . $cat_id . '"', $found );
} }
/**
* @ticket 32330
*/
public function test_wp_dropdown_categories_selected_should_respect_custom_value_field() {
$c1 = $this->factory->category->create( array(
'name' => 'Test Category 1',
'slug' => 'test_category_1',
) );
$c2 = $this->factory->category->create( array(
'name' => 'Test Category 2',
'slug' => 'test_category_2',
) );
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
'selected' => 'test_category_2',
) );
$this->assertContains( "value=\"test_category_2\" selected=\"selected\"", $found );
}
} }