Introduce required argument for wp_dropdown_categories().

This allows the HTML5 `required` attribute to be added to the `select` element.

Props wzislam, pcarvalho.
Fixes #31909.

git-svn-id: https://develop.svn.wordpress.org/trunk@37465 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-05-19 02:38:33 +00:00
parent 510bbfcb9c
commit 63755028e8
2 changed files with 74 additions and 1 deletions
+6 -1
View File
@@ -314,6 +314,7 @@ function category_description( $category = 0 ) {
*
* @since 2.1.0
* @since 4.2.0 Introduced the `value_field` argument.
* @since 4.6.0 Introduced the `required` argument.
*
* @param string|array $args {
* Optional. Array or string of arguments to generate a categories drop-down element.
@@ -351,6 +352,8 @@ function category_description( $category = 0 ) {
* @type string|array $taxonomy Name of the category or categories to retrieve. Default 'category'.
* @type bool $hide_if_empty True to skip generating markup if no categories are found.
* Default false (create select element even if no categories are found).
* @type bool $required Whether the <select> element should have the HTML5 'required' attribute.
* Default false.
* }
* @return string HTML content only if 'echo' argument is 0.
*/
@@ -376,6 +379,7 @@ function wp_dropdown_categories( $args = '' ) {
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => false,
);
$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
@@ -414,9 +418,10 @@ function wp_dropdown_categories( $args = '' ) {
$name = esc_attr( $r['name'] );
$class = esc_attr( $r['class'] );
$id = $r['id'] ? esc_attr( $r['id'] ) : $name;
$required = $r['required'] ? 'required' : '';
if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
$output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n";
} else {
$output = '';
}