WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#41882 new defect (bug)

Walker_CategoryDropdown does not wrap options inside HTML select element with wp_list_categories

Reported by: subrataemfluence Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.8.1
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

If I use Walker_CategoryDropdown() class to build a dropdown list with wp_list_categories function, the output does not get wrapped inside <select>...</select> element. The rendered output is like this:

<option class="level-0" value="16">Comedy</option>
<option class="level-0" value="17">Sci-Fi</option>

when it should be like

<select>
   <option class="level-0" value="16">Comedy</option>
   <option class="level-0" value="17">Sci-Fi</option>
</select>

In order to achieve this we can add an additional parameter html_wrapper_element in wp_list_categories function and pass a value dorpdown in the argument like:

<?php
<?php
/* Template Name: Movie List */
require_once ABSPATH . 'wp-admin/includes/template.php';
$args = array(
    'taxonomy' => 'movies_taxonomy',
    'walker' => new Walker_CategoryDropdown(),
    'hide_empty' => false,
    'html_wrapper_element' => 'dropdown'
);

wp_list_categories($args);
?>

and in wp_list_categories we add this before the $html gets returned from the function.

<?php
$html = apply_filters( 'wp_list_categories', $output, $args );
if( isset($r['html_wrapper_element'])  && $r['html_wrapper_element'] === 'dropdown') {
  $html = '<select>' . $html . '</select>';
}

if ( $r['echo'] ) {
  echo $html;
} else {
  return $html;
}

Attachments (1)

41882.diff (683 bytes) - added by subrataemfluence 2 months ago.

Download all attachments as: .zip

Change History (2)

#1 @subrataemfluence
2 months ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.