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 during search form generation, and the second step is to register this attribute as searchable.

add_action('em_template_events_search_form_ddm', '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 -->
	<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 ($_POST['style'] == $style_id) ? 'selected="selected"':''; ?>><?php echo $style_name; ?></option>
		<?php endforeach; ?>
	</select>
	<!-- END Styles Search -->
	<?php
}

We are hooking into the action em_template_events_search_form_ddm, which is called during the event search form generation 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.

The next step adds the ‘style’ attribute to accepted array of searches, which is done by adding a em_accepted_searches filter which is run before returning a list of accepted search attributes. This is to prevent unwanted search attributes accidentally making their way in, 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 attribute for style, Events Manager will now automatically detect the attribute if supplied during a search request.

In our next tutorial, we’re going to create a #_STYLES placeholder for our add-on by creating a custom placeholder.