Opened 8 years ago
Last modified 8 years ago
#46451 new defect (bug)
wp_dropdown_users() does not pass all applicable arguments on to get_users()
| Reported by: | pbiron | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Users | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
The docs for wp_dropdown_users() say of the $args param:
Array or string of arguments to generate a drop-down of users.
See WP_User_Query::prepare_query() for additional available arguments.
However, not every argument accepted by WP_User_Query::prepare_query() is passed on to get_users(): there is a specific whitelist, and some very useful args are not included in that whitelist (e.g., meta_query).
Instead, the arguments used by wp_dropdown_users() for generating the HTML markup of the dropdown should simply be removed from $args and anything left over should be passed to get_users().
Not only would this bring the behavior of wp_dropdown_users() into conformance with its docs, it would have the added benefit of future proofing it: if a future release adds additional args to get_users(), the whitelist in wp_dropdown_users() wouldn't have to be adjusted to accept them.
Change History (3)
#2
in reply to: ↑ 1
@
8 years ago
Replying to pbiron:
There are a couple of ways this could be achieved, I list a few below...and I'm sure there are others as well.
maybe the cleanest way would be to modify wp_array_slice_assoc() to accept an $invert param (which would be useful in and of itself):
<?php function wp_array_slice_assoc( $array, $keys, $invert = false ) { $slice = array(); if ( ! $invert ) { foreach ( $keys as $key ) { if ( isset( $array[ $key ] ) ) { $slice[ $key ] = $array[ $key ]; } } } else { foreach ( array_keys( $array ) as $key ) { if ( ! in_array( $key, $keys ) ) { $slice[ $key ] = $array[ $key ]; } } } return $slice; }
and then just use:
<?php $blacklist = array( 'show_option_all', 'show_option_none', 'hide_if_only_one_author', 'multi', 'show', 'echo', 'selected', 'name', 'class', 'id', 'include_selected', 'option_none_value', ); $query_args = wp_array_slice_assoc( $r, $blacklist, true ); ... $users = get_users( $query_args );
#3
@
8 years ago
What the xxxx was I thinking, the easiest way is:
<?php $blacklist = array( 'show_option_all', 'show_option_none', 'hide_if_only_one_author', 'multi', 'show', 'echo', 'selected', 'name', 'class', 'id', 'include_selected', 'option_none_value', ); $query_args = array_diff_key( $r, array_flip( $blacklist ) ); ... $users = get_users( $query_args );
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
There are a couple of ways this could be achieved, I list a few below...and I'm sure there are others as well.
Generate a whitelist from a blacklist
Using array_filter() with a named callback that strips the blacklist
where
_wp_dropdown_users_filter_get_users_args()is defined as:Using array_filter() with an anonymous callback that strips the blacklist