Adding Custom Event Search Form Fields
This tutorial forms part of our series of tutorials for creating an custom add-on for Events Manager called “Styles”, where we will be able to select from a list of styles during event registration.
We continue after having added a custom style attribute to your events, and now need to add a field to the search form so visitors can search and filter events by Style.
There's two steps to take here, one requires generating the form field in the events search form, and the second step is to register this parameter as searchable.
Generating the search form field
add_action('em_template_events_search_form_footer', 'my_em_styles_search_form');
function my_em_styles_search_form(){
$my_em_styles = (is_array(get_option('my_em_styles'))) ? get_option('my_em_styles'):array();
?>
<!-- START Styles Search -->
<div class="em-search-field">
<label>
<span>Styles</span>
<select name="style">
<option value=''>All Styles</option>
<?php foreach($my_em_styles as $style_id=>$style_name): ?>
<option value="<?php echo $style_id; ?>" <?php echo (!empty($_REQUEST['style']) && $_REQUEST['style'] == $style_id) ? 'selected="selected"':''; ?>><?php echo $style_name; ?></option>
<?php endforeach; ?>
</select>
</label>
</div>
<!-- END Styles Search -->
<?php
}
We are hooking into the action em_template_events_search_form_footer
, which is called in the 'advanced search' area of the search form and allows you to slot in new fields (or perform other actions).
This first function starts by loading the array of styles from the wp_options database into the $my_em_styles
variable. This variable is later looped through to show a select field of event styles, as well as checking if the style has already been requested in a previous search and whether to auto-select that style.
Allowing 'style' as an accepted search parameter
The next step adds the 'style' parameter to accepted array of searches, which is done by adding a em_accepted_searches
filter. This prevents unwanted search attributes accidentally making their way into our searches before a search is initiated, and so we need to make sure our attribute name makes it into that array.
function my_em_styles_accepted_searches($searches){
$searches[] = 'style';
return $searches;
}
add_filter('em_accepted_searches','my_em_styles_accepted_searches',1,1);
That's it! Since you've already created a custom search parameter for your events in our previous tutorial, Events Manager will now automatically detect the attribute if supplied during a search request.
Next Steps...
In our next tutorial, we're going to create a #_STYLES
placeholder for our add-on by creating a custom placeholder.