Make WordPress Core


Ignore:
Timestamp:
12/30/2014 09:30:16 PM (10 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/tests/phpunit/tests/category.php

    r28706 r31006  
    247247    }
    248248
     249    /**
     250     * @ticket 30306
     251     */
     252    public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
     253        // Create a test category.
     254        $cat_id = $this->factory->category->create( array(
     255            'name' => 'Test Category',
     256            'slug' => 'test_category',
     257        ) );
     258
     259        // Get the default functionality of wp_dropdown_categories().
     260        $dropdown_default = wp_dropdown_categories( array(
     261            'echo' => 0,
     262            'hide_empty' => 0,
     263        ) );
     264
     265        // Test to see if it returns the default with the category ID.
     266        $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
     267    }
     268
     269    /**
     270     * @ticket 30306
     271     */
     272    public function test_wp_dropdown_categories_value_field_term_id() {
     273        // Create a test category.
     274        $cat_id = $this->factory->category->create( array(
     275            'name' => 'Test Category',
     276            'slug' => 'test_category',
     277        ) );
     278
     279        // Get the default functionality of wp_dropdown_categories().
     280        $found = wp_dropdown_categories( array(
     281            'echo' => 0,
     282            'hide_empty' => 0,
     283            'value_field' => 'term_id',
     284        ) );
     285
     286        // Test to see if it returns the default with the category ID.
     287        $this->assertContains( 'value="' . $cat_id . '"', $found );
     288    }
     289
     290    /**
     291     * @ticket 30306
     292     */
     293    public function test_wp_dropdown_categories_value_field_slug() {
     294        // Create a test category.
     295        $cat_id = $this->factory->category->create( array(
     296            'name' => 'Test Category',
     297            'slug' => 'test_category',
     298        ) );
     299
     300        // Get the default functionality of wp_dropdown_categories().
     301        $found = wp_dropdown_categories( array(
     302            'echo' => 0,
     303            'hide_empty' => 0,
     304            'value_field' => 'slug',
     305        ) );
     306
     307        // Test to see if it returns the default with the category slug.
     308        $this->assertContains( 'value="test_category"', $found );
     309    }
     310
     311    /**
     312     * @ticket 30306
     313     */
     314    public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
     315        // Create a test category.
     316        $cat_id = $this->factory->category->create( array(
     317            'name' => 'Test Category',
     318            'slug' => 'test_category',
     319        ) );
     320
     321        // Get the default functionality of wp_dropdown_categories().
     322        $found = wp_dropdown_categories( array(
     323            'echo' => 0,
     324            'hide_empty' => 0,
     325            'value_field' => 'foo',
     326        ) );
     327
     328        // Test to see if it returns the default with the category slug.
     329        $this->assertContains( 'value="' . $cat_id . '"', $found );
     330    }
    249331}
Note: See TracChangeset for help on using the changeset viewer.