I'm using wp_dropdown_categories to create a shortcode that displays a search box that you can search categories given in the shortcode, such as [category-search include="1,2,3" selected="1"]
. This would search category ID's 1,2,3 and have category ID 1 selected by default.
Here's the function:
function category_search($atts) {
$atts = shortcode_atts( array(
'include' => '',
'selected' => '',
), $atts );
$site_url = site_url();
$args = '"include=array( ' . $atts['include'] . ')&selected=' . $atts['selected'] . '"';
ob_start(); ?>
<form role="search" method="get" id="searchform" action="<?php echo $site_url ?>">
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" placeholder="Search" value="" name="s" id="s" />
<?php wp_dropdown_categories( $args ); ?>
<button type="submit" class="button"><span>Search</span></button>
</form>
<?php return ob_get_clean();
}
add_shortcode( 'category-search', 'category_search' );
It works in that it displays a search box with a dropdown with all categories, but doesn't seem to pick up on the $args
with specific categories or default selection. Am I missing something?
You are setting the $args as a string when it should be an array.
Try: $args = array('include' => $atts['include'], 'selected' => $atts['selected'])