Build a Search UI
Algolia doesn’t provide support for WordPress or any WordPress plugin.
If you didn’t index your WordPress content with Algolia yet, see Setting up Algolia.
The easiest way to add a search UI to your WordPress website is by adding InstantSearch.js.
InstantSearch is also available for frontend frameworks.
Include InstantSearch in your theme
-
Download the following libraries and save them in the
themes/<your_theme>/js/vendor/
directory: -
Register both assets in your theme’s
functions.php
file.Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
function algolia_load_assets() { $clientPath = '/js/vendor/algoliasearch-lite.umd.js'; $instantSearchPath = '/js/vendor/instantsearch.production.min.js'; // Create a version number based on the last time the file was modified $clientVersion = date("ymd-Gis", filemtime( get_template_directory() . $clientPath)); $instantSearchVersion = date("ymd-Gis", filemtime( get_template_directory() . $instantSearchPath)); wp_enqueue_script('algolia-client', get_template_directory_uri() . $clientPath, array(), $clientVersion, true); wp_enqueue_script('algolia-instant-search', get_template_directory_uri() . $instantSearchPath, array('algolia-client'), $instantSearchVersion, true); // Optional: default styles for InstantSearch wp_enqueue_style('algolia-theme', get_template_directory_uri() . '/satellite-min.css'); } add_action('wp_enqueue_scripts', 'algolia_load_assets');
Add HTML containers
-
Add a container for the search box to the header of your theme:
Copy1
<div id="searchbox"></div>
-
Add container elements for the search results and tags to the page where you want to display search results:
Copy1 2
<div id="hits"></div> <div id="tags-list"></div>
Set up InstantSearch
-
Create a new file
js/algolia-search.js
and add it to thealgolia_load_assets
function:Copy1 2 3 4 5 6 7
function algolia_load_assets() { // ... $algoliaPath = '/js/algolia-search.js'; $algoliaVersion = date("ymd-Gis", filemtime(get_template_directory() . $algoliaPath)); wp_enqueue_script('algolia-search', get_template_directory_uri() . $algoliaPath, array('algolia-instant-search'), $algoliaVersion, true); }
-
Add the following code to the
js/algolia-search.js
file. The example assumes that the records in your Algolia index havetitle
,content
andtags
attributes.To make the search work, you must declare these attributes as
searchableAttributes
. Thetags
attribute should be included in theattributesForFaceting
.Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const searchClient = algoliasearch("YourApplicationID", "YourSearchOnlyAPIKey"); const search = instantsearch({ indexName: "YourIndexName", searchClient, searchFunction(helper) { // Ensure we only trigger a search when there's a query if (helper.state.query) { helper.search(); } }, }); search.addWidgets([ instantsearch.widgets.searchBox({ container: "#searchbox", }), instantsearch.widgets.refinementList({ container: "#tags-list", attribute: "tags", limit: 5, showMore: true, }), instantsearch.widgets.hits({ container: "#hits", templates: { item: ` <article> <a href="{{ url }}"> <strong> {{#helpers.highlight}} { "attribute": "title", "highlightedTagName": "mark" } {{/helpers.highlight}} </strong> </a> {{#content}} <p>{{#helpers.snippet}}{ "attribute": "content", "highlightedTagName": "mark" }{{/helpers.snippet}}</p> {{/content}} </article> `, }, }), ]); search.start();
Send click and conversion events
Complete your Algolia integration by sending click and conversion events. To learn more about events, see Get started with events.