Skip to main content

Adding Custom Event Search Form Fields

Build an add-on

Part 6 of 11 in the Build an add-on series. Previous: Search attributes. Next: Custom placeholder.

Now that events can be filtered by style, visitors should be able to do it themselves. This part adds a Style menu to the advanced options of the events search form.

You could do the same by overriding the search form templates. An action is the better choice for one extra field: it lives in your plugin, it survives theme changes, and there is no template copy to keep in step with plugin updates.

What you will build​

A select menu in the advanced search that submits a style value with the search.

Before you start

Complete part 5. The field only sends a value. Part 5 is what makes the search act on it. Advanced search must be switched on: in Events then Settings then Formatting then Search Form, set Enable advanced fields? to Yes.

Steps​

  1. Add this line inside my_em_styles_init():

    add_action( 'em_search_form_advanced_sections_footer', 'my_em_styles_search_form_field' );
  2. Add the function below to the end of the file.

Part 6: the search form field
function my_em_styles_search_form_field( $args ): void {
$styles = my_em_styles_get();
if ( ! $styles ) {
return;
}
$selected = absint( $args['style'] ?? ( $_REQUEST['style'] ?? 0 ) );
$field_id = 'em-search-style-' . absint( $args['id'] ?? 0 );
?>
<section class="em-search-section-styles em-search-advanced-section">
<header><?php esc_html_e( 'Style', 'my-em-styles' ); ?></header>
<div class="em-search-section-content">
<div class="em-search-style em-search-field">
<label for="<?php echo esc_attr( $field_id ); ?>" class="screen-reader-text"><?php esc_html_e( 'Style', 'my-em-styles' ); ?></label>
<select name="style" id="<?php echo esc_attr( $field_id ); ?>">
<option value=""><?php esc_html_e( 'All styles', 'my-em-styles' ); ?></option>
<?php foreach ( $styles as $style_id => $style_name ) : ?>
<option value="<?php echo esc_attr( $style_id ); ?>" <?php selected( $selected, (int) $style_id ); ?>><?php echo esc_html( $style_name ); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</section>
<?php
}

How it works​

The advanced search is built from sections, each a section element with the class em-search-advanced-section, a header, and a div.em-search-section-content holding one or more div.em-search-field wrappers. The em_search_form_advanced_sections_footer action fires after the last built-in section and passes the form's arguments. Printing a section with the same structure and classes gives your field the same layout and styling as the categories and tags sections.

The field's name is style, the attribute registered in part 5. When the form is submitted, Events Manager copies request values into the search only for attributes that get_default_search() knows, and part 5 added style to that list. Nothing else is needed to connect the two.

The selected option comes from the form's arguments when they include a style value, and from the request otherwise, so the menu keeps the visitor's choice after a search. Either value goes through absint(), and each option is escaped.

Older code

Tutorials for earlier versions hooked into em_template_events_search_form_footer and added style through the em_accepted_searches filter. The first is marked as deprecated in the 7.5 templates. The second still exists, but is not needed for an attribute registered through get_default_search().

To put a field in the main search bar instead of the advanced options, use the em_template_search_form_footer action, which fires before the search button.

Test it​

  1. Open your events page, or a page with [event_search_form], and open the advanced search options. A Style section is at the end.
  2. Choose Formal and search. The results contain only events with that style.
  3. Choose All styles and search again. All events return.

Next​

Part 7: Custom placeholder