Introduce `class` argument to `wp_dropdown_pages()`.

This new argument allows devs to specify the 'class' attribute of the select
element.

Props ramiy, voldemortensen.
Fixes #30082.

git-svn-id: https://develop.svn.wordpress.org/trunk@32727 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-06-12 13:04:04 +00:00
parent 671218ac1d
commit dba2b6a9c0
2 changed files with 42 additions and 1 deletions

View File

@ -990,6 +990,7 @@ function the_meta() {
*
* @since 2.1.0
* @since 4.2.0 The `$value_field` argument was added.
* @since 4.3.0 The `$class` argument was added.
*
* @param array|string $args {
* Optional. Array or string of arguments to generate a pages drop-down element.
@ -1002,6 +1003,7 @@ function the_meta() {
* @type string $name Value for the 'name' attribute of the select element.
* Default 'page_id'.
* @type string $id Value for the 'id' attribute of the select element.
* @type string $class Value for the 'class' attribute of the select element. Default: none.
* Defaults to the value of `$name`.
* @type string $show_option_none Text to display for showing no pages. Default empty (does not display).
* @type string $show_option_no_change Text to display for "no change" option. Default empty (does not display).
@ -1016,6 +1018,7 @@ function wp_dropdown_pages( $args = '' ) {
'depth' => 0, 'child_of' => 0,
'selected' => 0, 'echo' => 1,
'name' => 'page_id', 'id' => '',
'class' => '',
'show_option_none' => '', 'show_option_no_change' => '',
'option_none_value' => '',
'value_field' => 'ID',
@ -1031,7 +1034,12 @@ function wp_dropdown_pages( $args = '' ) {
}
if ( ! empty( $pages ) ) {
$output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "'>\n";
$class = '';
if ( ! empty( $r['class'] ) ) {
$class = " class='" . esc_attr( $r['class'] ) . "'";
}
$output = "<select name='" . esc_attr( $r['name'] ) . "'" . $class . " id='" . esc_attr( $r['id'] ) . "'>\n";
if ( $r['show_option_no_change'] ) {
$output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n";
}

View File

@ -198,6 +198,39 @@ NO;
$this->assertContains( 'value="' . $p . '"', $found );
}
/**
* @ticket 30082
*/
public function test_wp_dropdown_pages_should_not_contain_class_attribute_when_no_class_is_passed() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
) );
$this->assertNotRegExp( '/<select[^>]+class=\'/', $found );
}
/**
* @ticket 30082
*/
public function test_wp_dropdown_pages_should_obey_class_parameter() {
$p = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$found = wp_dropdown_pages( array(
'echo' => 0,
'class' => 'bar',
) );
$this->assertRegExp( '/<select[^>]+class=\'bar\'/', $found );
}
/**
* @ticket 31389
*/