diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
index 35d6c82..37ba533 100644
|
|
function category_description( $category = 0 ) { |
314 | 314 | * |
315 | 315 | * @since 2.1.0 |
316 | 316 | * @since 4.2.0 Introduced the `value_field` argument. |
| 317 | * @since 4.6.0 Added required attribute |
317 | 318 | * |
318 | 319 | * @param string|array $args { |
319 | 320 | * Optional. Array or string of arguments to generate a categories drop-down element. |
… |
… |
function category_description( $category = 0 ) { |
351 | 352 | * @type string|array $taxonomy Name of the category or categories to retrieve. Default 'category'. |
352 | 353 | * @type bool $hide_if_empty True to skip generating markup if no categories are found. |
353 | 354 | * Default false (create select element even if no categories are found). |
| 355 | * @type bool $required Whether the field is required or not using HTML5 'required' attribute. |
| 356 | * Accepts 0, 1, or their boolean. Defaults to false. |
354 | 357 | * } |
355 | 358 | * @return string HTML content only if 'echo' argument is 0. |
356 | 359 | */ |
357 | 360 | function wp_dropdown_categories( $args = '' ) { |
358 | 361 | $defaults = array( |
359 | | 'show_option_all' => '', 'show_option_none' => '', |
360 | | 'orderby' => 'id', 'order' => 'ASC', |
| 362 | 'show_option_all' => '', |
| 363 | 'show_option_none' => '', |
| 364 | 'option_none_value' => -1, |
| 365 | 'orderby' => 'id', |
| 366 | 'order' => 'ASC', |
361 | 367 | 'show_count' => 0, |
362 | | 'hide_empty' => 1, 'child_of' => 0, |
363 | | 'exclude' => '', 'echo' => 1, |
364 | | 'selected' => 0, 'hierarchical' => 0, |
365 | | 'name' => 'cat', 'id' => '', |
366 | | 'class' => 'postform', 'depth' => 0, |
367 | | 'tab_index' => 0, 'taxonomy' => 'category', |
368 | | 'hide_if_empty' => false, 'option_none_value' => -1, |
| 368 | 'hide_empty' => 1, |
| 369 | 'child_of' => 0, |
| 370 | 'exclude' => '', |
| 371 | 'echo' => 1, |
| 372 | 'selected' => 0, |
| 373 | 'hierarchical' => 0, |
| 374 | 'name' => 'cat', |
| 375 | 'id' => '', |
| 376 | 'class' => 'postform', |
| 377 | 'depth' => 0, |
| 378 | 'tab_index' => 0, |
| 379 | 'taxonomy' => 'category', |
| 380 | 'hide_if_empty' => false, |
369 | 381 | 'value_field' => 'term_id', |
| 382 | 'required' => false |
370 | 383 | ); |
371 | 384 | |
372 | 385 | $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
… |
… |
function wp_dropdown_categories( $args = '' ) { |
405 | 418 | $name = esc_attr( $r['name'] ); |
406 | 419 | $class = esc_attr( $r['class'] ); |
407 | 420 | $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
| 421 | $required = $r['required'] ? 'required' : ''; |
408 | 422 | |
409 | 423 | if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
410 | | $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
| 424 | $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
411 | 425 | } else { |
412 | 426 | $output = ''; |
413 | 427 | } |
diff --git tests/phpunit/tests/category.php tests/phpunit/tests/category.php
index 913f4dc..aff9c1d 100644
|
|
class Tests_Category extends WP_UnitTestCase { |
262 | 262 | // Test to see if it returns the default with the category ID. |
263 | 263 | $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default ); |
264 | 264 | } |
| 265 | /** |
| 266 | * @ticket 31909 |
| 267 | */ |
| 268 | public function test_wp_dropdown_categories_value_field_should_contain_required() { |
| 269 | // Create a test category. |
| 270 | $cat_id = self::factory()->category->create( array( |
| 271 | 'name' => 'Test Category', |
| 272 | 'slug' => 'test_category', |
| 273 | ) ); |
| 274 | |
| 275 | // Get the default functionality of wp_dropdown_categories(). |
| 276 | $args = array( |
| 277 | 'show_option_none' => __( 'Select one', 'text-domain' ), |
| 278 | 'option_none_value' => "", |
| 279 | 'required' => 1, |
| 280 | 'hide_empty' => 0, |
| 281 | 'echo' => 0, |
| 282 | ); |
| 283 | $dropdown_categories = wp_dropdown_categories( $args ); |
| 284 | |
| 285 | |
| 286 | // Test to see if it contains the "required" attribute. |
| 287 | $this->assertContains( 'required', $dropdown_categories ); |
| 288 | } |
265 | 289 | |
266 | 290 | /** |
267 | 291 | * @ticket 30306 |