Make WordPress Core


Ignore:
Timestamp:
12/30/2014 09:30:16 PM (9 years ago)
Author:
boonebgorges
Message:

In wp_dropdown_categories(), allow the term field used to populate option valuesto be specified.

The new 'value_field' parameter makes it possible to set term slugs (or some
other term property) as the 'value' attribute of the option elements generated
by wp_dropdown_categories(). This additional flexibility reduces the effort
required to translate term_id to other term fields when processing form
submissions that include values from taxonomy dropdowns. See #30865 for a
use case.

Props collinsinternet.
Fixes #30306.

File:
1 edited

Legend:

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

    r31005 r31006  
    294294 *
    295295 * @since 2.1.0
     296 * @since 4.2.0 Introduced the 'value_field' parameter.
    296297 *
    297298 * @param string|array $args {
     
    328329 *                                           Defaults to the value of $name.
    329330 *     @type string       $class             Optional. Value for the 'class' attribute of the select element.
    330  *     @type int          $selected          Optional. ID of the category to be selected.
     331 *     @type int|string   $selected          Optional. Value of the option that should be selected.
     332 *     @type string       $value_field       Optional. Term field that should be used to populate the 'value' attribute
     333 *                                           of the option elements. Accepts any valid term field: 'term_id', 'name',
     334 *                                           'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
     335 *                                           'parent', 'count'. Default 'term_id'.
    331336 *     @type string       $taxonomy          Optional. Name of the category to retrieve. Default 'category'.
    332337 *     @type bool         $hide_if_empty     Optional. True to skip generating markup if no categories are found.
     
    347352        'class' => 'postform', 'depth' => 0,
    348353        'tab_index' => 0, 'taxonomy' => 'category',
    349         'hide_if_empty' => false, 'option_none_value' => -1
     354        'hide_if_empty' => false, 'option_none_value' => -1,
     355        'value_field' => 'term_id',
    350356    );
    351357
     
    11071113     * @param object $category Category data object.
    11081114     * @param int    $depth    Depth of category. Used for padding.
    1109      * @param array  $args     Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories()
     1115     * @param array  $args     Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
     1116     *                         See {@see wp_dropdown_categories()}.
    11101117     */
    11111118    public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
     
    11151122        $cat_name = apply_filters( 'list_cats', $category->name, $category );
    11161123
    1117         $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
     1124        if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) {
     1125            $args['value_field'] = 'term_id';
     1126        }
     1127
     1128        $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\"";
     1129
    11181130        if ( $category->term_id == $args['selected'] )
    11191131            $output .= ' selected="selected"';
Note: See TracChangeset for help on using the changeset viewer.