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.

git-svn-id: https://develop.svn.wordpress.org/trunk@31006 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2014-12-30 21:30:16 +00:00
parent 06b820a989
commit ae6575685c
2 changed files with 99 additions and 5 deletions

View File

@@ -246,4 +246,86 @@ class Tests_Category extends WP_UnitTestCase {
$this->assertNull( get_category_by_path( 'nocat/nocat/', false) );
}
}
/**
* @ticket 30306
*/
public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
// Create a test category.
$cat_id = $this->factory->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
// Get the default functionality of wp_dropdown_categories().
$dropdown_default = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
) );
// Test to see if it returns the default with the category ID.
$this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
}
/**
* @ticket 30306
*/
public function test_wp_dropdown_categories_value_field_term_id() {
// Create a test category.
$cat_id = $this->factory->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'term_id',
) );
// Test to see if it returns the default with the category ID.
$this->assertContains( 'value="' . $cat_id . '"', $found );
}
/**
* @ticket 30306
*/
public function test_wp_dropdown_categories_value_field_slug() {
// Create a test category.
$cat_id = $this->factory->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'slug',
) );
// Test to see if it returns the default with the category slug.
$this->assertContains( 'value="test_category"', $found );
}
/**
* @ticket 30306
*/
public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
// Create a test category.
$cat_id = $this->factory->category->create( array(
'name' => 'Test Category',
'slug' => 'test_category',
) );
// Get the default functionality of wp_dropdown_categories().
$found = wp_dropdown_categories( array(
'echo' => 0,
'hide_empty' => 0,
'value_field' => 'foo',
) );
// Test to see if it returns the default with the category slug.
$this->assertContains( 'value="' . $cat_id . '"', $found );
}
}