WordPress dropdown multiselect option

Nikoleta picture Nikoleta · Mar 15, 2015 · Viewed 8.5k times · Source

I have the below code for WordPress dropdown single select option. I want to make this code work with multiple selection option, but dont know how. Please give a solution.

<?php 
        $args=array(
                'class'       => 'select-submit2',
                'hide_empty'  => false,
                'selected'    => $prop_action_category_selected,
                'name'        => 'prop_action_category',
                'id'          => 'prop_action_category_submit',
                'orderby'     => 'NAME',
                'order'       => 'ASC',
                'show_option_none'   => __('None','wordpress'),
                'taxonomy'    => 'property_action_category',
                'hierarchical'=> true
            );

           wp_dropdown_categories( $args );  ?>

Kind regards

Nikoleta

Answer

butlerblog picture butlerblog · Mar 15, 2015

The wp_dropdown_categories() function is a WordPress function that creates a dropdown of categories. You can specify how and what you want it to output based on passing various arguments to the function.

However, there is no argument that the function accepts to change it from a single to a multiple select list.

A simple approach is to alter the output that the function gives you after it is generated. This isn't always the best approach to everything, but in this case, there are two key elements that make this a simple approach:

  1. While the function echoes (prints) its output by default, it accepts an argument to simply return the HTML result without outputting it (allowing you to change it before it is displayed).
  2. Changing a dropdown to a multiple select list is as simple as adding "multiple" to the HTML tag (i.e. changing <select> to <select multiple>)

You can call the function, placing the results in a variable before it is output, then use PHP's str_replace() on that result to slip "mulitple" into the select tag:

/**
 * Your args from the question
 * plus turning echo off.
 * Note the change to the name (adding "[]")
 */
$args = array(
    'class'       => 'select-submit2',
    'hide_empty'  => false,
    'selected'    => $prop_action_category_selected,
    'name'        => 'prop_action_category[]',
    'id'          => 'prop_action_category_submit',
    'orderby'     => 'NAME',
    'order'       => 'ASC',
    'show_option_none'   => __('None','wpestate'),
    'taxonomy'    => 'property_action_category',
    'hierarchical'=> true,
    'echo'        => 0,
);

/** get the dropdown **/
$dropdown = wp_dropdown_categories( $args );

/** insert "multiple" using str_replace **/
$multi = str_replace( '<select', '<select multiple ', $dropdown );

/** output result **/
echo $multi;

Passing the "echo" argument of "0" tells the function not to output anything (add any other arguments to the array as needed). Then str_replace() is run on the result, and the result of that is what you output.

Note that you'll need to change the "name" argument to pass an array to be able to pass/get all of the selected items.