Support Home > Developers > Omnisearch

Omnisearch

Omnisearch makes it easy to search for anything, both locally on your site, and from select providers on the web!

Using Omnisearch

Using Omnisearch is remarkably easy. Just search! If you’re in your WordPress Admin Panel, you can use the search bar in the WordPress Admin Bar (top right, just beyond your User Menu), or go to Jetpack  Omnisearch and start from there. Please note that the Search field on the WordPress Admin Bar will only Omnisearch from the back-end of your site, as the front-end already has a search field there to perform a site search natively, and we didn’t want to change that for you.

Adding a Results section

Adding a new section of results to Omnisearch is incredibly easy. At its most basic, all you need to do is:

add_filter( 'omnisearch_results', 'my_omnisearch_results', 10, 2 );
function my_omnisearch_results( $results, $search_term ) {
	$html = '<h2>My Omnisearch Results</h2>';
	$html .= '<p>You just searched for <em>' . esc_html( $search_term ) . '</em>!</p>';

	$results['My Results'] = $html;
	return $results;
}

A few things to note:

  • Two things will be passed to the filter. The first is the array of results that you will add your results to. The second is the search string. Remember to escape your user inputs!
  • The array key that you assign your results HTML to (in this case, 'My Results') will be used to generate the ‘Jump To’ link at the top of the page, and the ID of the element containing your results. Please choose something unique!
  • You can be as free with your HTML as you like. We’ve used WP_List_Tables to generate our content because we like the look of it, but if you’d rather present your results in a list of floated divs, feel free.
  • If you’d like to use the same maximum number of results as everything else, use apply_filters( 'omnisearch_num_results', 5 ); to get it.
  • If you’d like to override the maximum number of results that each module provides, just hook into the omnisearch_num_results filter.
  • If you’re doing a search that you expect to take a while, such as hitting an external API, you may want to load your results in via AJAX. We’ve already set the search_term and num_results globals in Javascript for you, and you can see an example of loading results in via AJAX in the Jetpack_Omnisearch_Plugins class here.

Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 58,049 other followers