Last active
March 16, 2019 14:00
-
-
Save MahdiY/a4f1a4eb546ad8461d1e7e531ac7b065 to your computer and use it in GitHub Desktop.
wp_dropdown_categories with multiple select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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