Strattic Developers by Elementor

How to Set up an Algolia Instasearch App on your Strattic Site

Before you get started

This guide assumes you’re bringing your own Algolia index, and not using Strattic’s index of your site.

From there, you will need three things for the app to work:

  • Application ID (e.g. ABCDEF12345)
  • Search API key (e.g. asdf1234asdf1234asdf1234)
  • Index name (e.g. wp_posts_index)

These can all be found in your Algolia dashboard.

A note on the publish process on Strattic

On Strattic, you have the Live and Preview sites and if there’s anything unique you need to do, make sure to account for that.

For example, after you change the styling for your search, you’d need to republish to Preview or Live to see the changes on your static site. But if you’re just refreshing Algolia results or reindexing, you shouldn’t need to republish.

Set up a local working version of your app (optional, but helpful)

I’d recommend getting set up locally first, which will allow you to iterate quickly to get the functionality you’re looking for (we’ll talk about styles more below).

See Examples here:

Review what Instantsearch is:

Follow the Gettings Started guide to get up and running with an app:

If you’re using the command line and npx create-instantsearch-app my-app, you’ll need to make sure you have NodeJS installed on your local computer (see for more info and download and installation instructions).

From here, you can go through the npx create-instantsearch-app my-app, entering:

  • The name of the application or widget: my-app (or whatever you want to name it)
  • InstantSearch template: InstantSearch.js (you can use React or something else if you like, but the JS version is what is demoed below)
  • InstantSearch.js version: 4.37.1 (or whatever the latest version is)
  • Application ID: latency (or whatever you app ID is)
  • Search API key: 6be0576ff61c053d5f9a3225e2a90f76 (or whatever you API key is)
  • Index name: instant_search (or whatever you index name is)
  • Attributes to display: You can select as many of these as you like.
  • Dynamic widget attributes: Start just selecting one and add more as you go along (the below code doesn’t use dynamic widgets, so you could also select none here)

Create the app, and follow the terminal instructions for starting the app.

See the following for more info and hints on building out the app (and to understand the below code):

You may need to transform the hits items coming back from Algolia. If that’s the case, you can use this method: (see below code for more on this method).

A note on the HTML search elements…

You’ll notice that some elements are populated by the JavaScript, not the HTML. These elements include:

  • Search input box
  • Search button
  • Tags and category checkboxes
  • Individual result boxes

If you need more control over the layout and interactivity of these boxes, you might consider using the Algolia API more directly.

Move it to WordPress

Now that the app is built out, let’s add it to our WordPress.

This is adapted from:

Assuming you have a child theme set up, do the following:

  1. Add a page-instasearch.php file to your child theme (code below).
  2. Create a page in your WordPress dashboard called Instasearch and chose the Instasearch page themplate.
  3. In an instasearch directory in your child theme folder add two files: instastyles.css and instascripts.js (code below)
  4. In your child theme’s function.php file, add the function and action hook that enqueues the appropriate scripts (code below)

The result can be previewed here:

Note, you can add whatever JS or CSS code you added to app.js to your instascripts.js script (which adjustsments for the window, see the above directly-in-you-page link), and the CSS to instastyles.css.

Also, you can call these files whatever you like, feel free 😁.

With the below code, you should be able to see the Instasearch app show up on whatever page you’ve assigned it to on your WordPress site.

A note on styles: If you look at the Preview on Strattic, the styles are different and could “use some work”. That’s fine, and up to you to make sure things are styled to your needs, though the Instasearch styles we’ve enqueued give you a good starting point. You can read more about about styling Instasearch here:

The page-instasearch.php template


 * Template Name: Instasearch
 * @link
 * @package WordPress
 * @subpackage Twenty_Twenty
 * @since Twenty Twenty 1.0


<main id="site-content" role="main">

    <div class="ais-InstantSearch">
        <h1>InstantSearch Demo</h1>

        <div class="left-panel">
            <div id="clear-refinements"></div>

            <div id="taxonomy-category"></div>
            <div id="taxonomy-post_tag"></div>

        <div class="right-panel">
            <div id="searchbox"></div>
            <div id="hits"></div>
            <div id="pagination"></div>

    <div class="container">
        <div class="search-panel">
            <div class="search-panel__filters">
                <div id="dynamic-widgets"></div>

            <div class="search-panel__results">
                <div id="searchbox"></div>
                <div id="hits"></div>

        <div id="pagination"></div>

</main><!-- #site-content -->

<?php get_template_part('template-parts/footer-menus-widgets'); ?>

<?php get_footer(); ?>

The instastyles.css file

.ais-ClearRefinements {
  margin: 1em 0;
.ais-SearchBox {
  margin: 1em 0;
.ais-Pagination {
  margin-top: 1em;
.left-panel {
  float: left;
  width: 250px;
.right-panel {
  margin-left: 260px;
.ais-InstantSearch {
  max-width: 960px;
  overflow: hidden;
  margin: 0 auto;
.ais-Hits-item {
  margin-bottom: 1em;
  width: calc(50% - 1rem);
.ais-Hits-item img {
  margin-right: 1em;
.hit-name {
  margin-bottom: 0.5em;
.hit-description {
  color: #888;
  font-size: 14px;
  margin-bottom: 0.5em;

The instascripts.js file

const { algoliasearch, instantsearch } = window;

const searchClient = algoliasearch('ABCDEF12345', 'asdf1234asdf1234asdf1234');

const search = instantsearch({
  indexName: 'wp_posts_index',

    container: '#searchbox',
    container: '#clear-refinements',
    container: '#taxonomy-category',
    attribute: 'taxonomies.category',
    container: '#taxonomy-post_tag',
    attribute: 'taxonomies.post_tag',
    hitsPerPage: 12,
    attributesToRetrieve: [
      '*', // list of attributes to retrieve
    container: '#hits',
    transformItems(items) {
      return => ({
        post_title: item.post_title.toLowerCase(),
        categories: ( item.taxonomies.category ) ? item.taxonomies.category.join(', ') : '',
        post_tags: ( item.taxonomies.post_tag ) ? item.taxonomies.post_tag.join(', ') : '',
        featureImg: ( item.images.hasOwnProperty('thumbnail') ) ? item.images.thumbnail.url.replace('', '') : ''
    templates: {
      item: `
          {{#featureImg}}<img src="{{ featureImg }}" align="left" alt="{{post_title}}" /> {{/featureImg}}
          <h3>{{#helpers.highlight}}{ "attribute": "post_title" }{{/helpers.highlight}}</h3>
          <p>Author: {{ post_author.display_name   }}</p>
          <p>Categories: {{ categories }} | Tags: {{ post_tags }}</p>
          <p>{{#helpers.highlight}}{ "attribute": "content" }{{/helpers.highlight}}</p>
    container: '#pagination',


Add this snippet to your function.php file

function strattic_instasearch_register_styles_scripts() {
    if( is_page( 'instasearch' ) ) {

        $theme_version = wp_get_theme()->get( 'Version' );

        // Add app styles.
        wp_enqueue_style( 'strattic_instasearch-rest-style', '//', null, $theme_version );
        wp_enqueue_style( 'strattic_instasearch-base-style', '//', null, $theme_version );
        wp_enqueue_style( 'strattic_instasearch-print-style', get_stylesheet_directory_uri() . '/instasearch/instastyles.css', null, $theme_version );

        // Add app script.
        wp_enqueue_script( 'strattic_instasearch-algolia-lite-js', '//', array(), $theme_version, false );
        wp_script_add_data( 'strattic_instasearch-js', 'async', true );
        wp_enqueue_script( 'strattic_instasearch-prod-js', '//', array(), $theme_version, false );
        wp_script_add_data( 'strattic_instasearch-js', 'async', true );
        wp_enqueue_script( 'strattic_instasearch-js', get_stylesheet_directory_uri() . '/instasearch/instascripts.js', array(), $theme_version, false );
        wp_script_add_data( 'strattic_instasearch-js', 'async', true );

add_action( 'wp_enqueue_scripts', 'strattic_instasearch_register_styles_scripts' );