Opened 9 years ago
Closed 9 years ago
#33452 closed defect (bug) (fixed)
wp_dropdown_categories doesn't respect show_option_all
Reported by: | dfellini | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.3.1 | Priority: | normal |
Severity: | normal | Version: | 4.3 |
Component: | Taxonomy | Keywords: | fixed-major |
Focuses: | Cc: |
Description
Using the following code:
wp_dropdown_categories('show_option_all=All Locations&name=location&show_count=1&orderby=name&echo=1&taxonomy=joblocation&value_field=slug');
... yields me a list of states in correct order with 'All Locations' properly selected by default, up until the 4.3 upgrade. Now it has the very last item in the list selected by default.
Tested by running plugin on 4.2.4 where it displayed as expected, then upgrading to 4.3, where the problem was introduced.
Attachments (2)
Change History (9)
#2
@
9 years ago
- Keywords has-patch needs-testing added
- Milestone changed from Awaiting Review to 4.3.1
Hi dfellini - Welcome to Trac, and thanks for the report!
I've confirmed the issue. It dates from [32484]. This odd bug arises due to an unfortunate combination of (a) the way that the 'selected' argument works, and (b) the way PHP interprets ==
between an integer and a string.
More specifically: The default 'selected' value in wp_dropdown_categories()
is 0
. In Walker_CategoryDropdown::start_el()
, we do a comparison if ( $category->{$value_field} == $args['selected'] )
. Because you're using slug
as your 'value_field'
, the check looks something like this: if ( 'connecticut' == 0 )
. The problem is that PHP resolves 'connecticut' == 0
to true
; see http://stackoverflow.com/questions/6843030/why-does-php-consider-0-to-be-equal-to-a-string for some explanation. So each <option>
in your dropdown is getting selected="selected"
.
The most straightforward fix is to cast everything to a string, and to do a strict comparison. See 33452.diff. Can you have a look to see whether this solves your problem?
This is going to be a relatively large annoyance for people who are relying on the default value of 'selected' in wp_dropdown_categories()
, so I'm moving the ticket to 4.3.1 to address in the next minor release.
#4
@
9 years ago
- Owner set to boonebgorges
- Resolution set to fixed
- Status changed from new to closed
In 33681:
#5
@
9 years ago
- Keywords fixed-major added; has-patch needs-testing removed
- Resolution fixed deleted
- Status changed from closed to reopened
Reopening for 4.3.1.
Screenshot of category list with wrong default selected