Introduce 'value_field' parameter to wp_dropdown_pages().

This parameter allows developers to choose the post field that will be used to
fill in the 'option' attribute of the generated dropdown markup.

See [31006] #30306 for a parallel enhancement in `wp_dropdown_categories()`.

Props jfarthing84.
Fixes #12494.

git-svn-id: https://develop.svn.wordpress.org/trunk@31338 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-02-05 19:03:52 +00:00
parent a710c0451c
commit c9c9af5df8
2 changed files with 78 additions and 6 deletions

View File

@ -971,7 +971,8 @@ function wp_dropdown_pages( $args = '' ) {
'selected' => 0, 'echo' => 1,
'name' => 'page_id', 'id' => '',
'show_option_none' => '', 'show_option_no_change' => '',
'option_none_value' => ''
'option_none_value' => '',
'value_field' => 'ID',
);
$r = wp_parse_args( $args, $defaults );
@ -1425,15 +1426,20 @@ class Walker_PageDropdown extends Walker {
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $page Page data object.
* @param int $depth Depth of page in reference to parent pages. Used for padding.
* @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option element.
* @param object $page Page data object.
* @param int $depth Depth of page in reference to parent pages. Used for padding.
* @param array $args Uses 'selected' argument for selected page to set selected HTML attribute for option
* element. Uses 'value_field' argument to fill "value" attribute. See {@see wp_dropdown_pages()}.
* @param int $id
*/
public function start_el( &$output, $page, $depth = 0, $args = array(), $id = 0 ) {
$pad = str_repeat(' ', $depth * 3);
$output .= "\t<option class=\"level-$depth\" value=\"$page->ID\"";
if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
$args['value_field'] = 'ID';
}
$output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $page->{$args['value_field']} ) . "\"";
if ( $page->ID == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';

View File

@ -131,4 +131,70 @@ NO;
) );
$this->assertEquals( $option_no_change, $output );
}
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_should_default_to_ID() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
) );
// Should contain page ID by default.
$this->assertContains( 'value="' . $p . '"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_ID() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
'value_field' => 'ID',
) );
$this->assertContains( 'value="' . $p . '"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_post_name() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
'value_field' => 'post_name',
) );
$this->assertContains( 'value="foo"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_should_fall_back_on_ID_when_an_invalid_value_is_provided() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
'value_field' => 'foo',
) );
$this->assertContains( 'value="' . $p . '"', $found );
}
}