Skip to content

Instantly share code, notes, and snippets.

@MahdiY
Last active March 16, 2019 14:00
Show Gist options
  • Save MahdiY/a4f1a4eb546ad8461d1e7e531ac7b065 to your computer and use it in GitHub Desktop.
Save MahdiY/a4f1a4eb546ad8461d1e7e531ac7b065 to your computer and use it in GitHub Desktop.
wp_dropdown_categories with multiple select
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
<?php
add_filter( 'wp_dropdown_cats', 'wp_dropdown_cats_multiple', 10, 2 );
function wp_dropdown_cats_multiple( $output, $r ) {
if( isset( $r['multiple'] ) && $r['multiple'] ) {
$output = preg_replace( '/^<select/i', '<select multiple', $output );
$output = str_replace( "name='{$r['name']}'", "name='{$r['name']}[]'", $output );
foreach ( array_map( 'trim', explode( ",", $r['selected'] ) ) as $value )
$output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment