Skip to content

Instantly share code, notes, and snippets.

@parsakafi
Last active December 3, 2017 15:36
Show Gist options
  • Save parsakafi/000e1fe4c42cd18526096dc34b9e1731 to your computer and use it in GitHub Desktop.
Save parsakafi/000e1fe4c42cd18526096dc34b9e1731 to your computer and use it in GitHub Desktop.
wp_dropdown_categories multi select
<?php
function wp_dropdown_categories_pk($name, $taxonomy, $selected = array())
{
$select = wp_dropdown_categories(array(
'name' => $name,
'show_option_none' => '- Category -',
'option_none_value' => '-1',
'taxonomy' => $taxonomy,
'hierarchical' => 1,
'echo' => false
));
if (is_array($selected))
foreach ($selected as $sel)
if (!empty($sel) && $sel != '-1')
$select = str_replace('value="' . $sel . '"', 'value="' . $sel . '" selected', $select);
return $select;
}
// Active Multi Select
function dropdown_filter($output, $r)
{
$output = preg_replace('/<select (.*?) >/', '<select $1 size="5" multiple>', $output);
return $output;
}
add_filter('wp_dropdown_cats', 'dropdown_filter', 10, 2);
Categories:
<?php echo wp_dropdown_categories_pk('multiple_categories[]', 'category', array(10,12)); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment