Customize Autocomplete drop-down
Customize your drop-down menu that appears underneath every search bar of your WordPress website.
Introduction
The autocomplete experience can be enabled in the Autocomplete section of the plugin.
The autocomplete dropdown menu is powered by autocomplete.js, a JavaScript library that eases the process of building rich dropdown menus.
Please checkout the github repository of the autocomplete.js library.
Customization
To start customizing your dropdown menu, copy/paste the wp-content/plugins/algolia/templates
directory in your own theme, and rename it to algolia
. If your theme is named mytheme
, then the folder should look like: wp-content/themes/mytheme/algolia
.
You can then edit the algolia/autocomplete.php
file to customize the autocomplete dropdown menu (suggestions, header, footer) templates & associated JavaScript code.
Edit the suggestion templates
Each autocomplete.js source will display its suggestions with an associated template. To configure them, edit the algolia/autocomplete.php
file and locate the tmpl-autocomplete-*-suggestion
templates:
Post template
This is the template used for all Post-based types.
<script type="text/html" id="tmpl-autocomplete-post-suggestion"></script>
Term template
This is the template used for all Term-based types.
<script type="text/html" id="tmpl-autocomplete-term-suggestion"></script>
User template
This is the template used for Users.
<script type="text/html" id="tmpl-autocomplete-user-suggestion"></script>
Edit the source headers
Each autocomplete.js source will have an associated header displayed before the suggestions. To configure it, edit the algolia/autocomplete.php
file and locate the tmpl-autocomplete-header
template:
<script type="text/html" id="tmpl-autocomplete-header"></script>
Edit the dropdown menu footer
The dropdown menu is provided with a default footer. To configure it, edit the algolia/autocomplete.php
file and locate the tmpl-autocomplete-footer
template:
<script type="text/html" id="tmpl-autocomplete-footer"></script>
Edit the no results message
When no results are found, autocomplete.js will display a specific template. To configure it, edit the algolia/autocomplete.php
file and locate the tmpl-autocomplete-empty
template:
<script type="text/html" id="tmpl-autocomplete-empty"></script>
Adding an extra source
In some cases, you may want to add an extra section to your dropdown menu to search in an external index (of another website you own for instance). In order to achieve that, edit the algolia/autocomplete.php
file and locate the autocomplete(...)
call. As you'll see, the 3rd parameter of the function is the array of sources
you're using.
To add an extra source, just append it to the existing sources
array, just before the autocomplete(...)
call:
// new source appended to the `sources` array
sources.push({
source: /* .... */,
templates: {
header: /* .... */,
suggestion: /* .... */
}
});
autocomplete(/* .... */);
You can read more about multi sources/categories autocomplete.js implementation in this tutorial.
Look & feel
We provide with some default CSS rules that are located in the algolia/css/algolia-autocomplete.css
. You can very easily add your own CSS rules to your theme's stylesheet.
If for any reason you don't want the default stylesheet to be included, you can remove it like this:
<?php
/**
* Dequeue default CSS files.
*
* Hooked to the wp_print_styles action, with a late priority (100),
* so that it is after the stylesheets were enqueued.
*/
function my_theme_dequeue_styles() {
// Remove the algolia-autocomplete.css.
wp_dequeue_style( 'algolia-autocomplete' );
}
add_action( 'wp_print_styles', 'my_theme_dequeue_styles', 100 );
Disable autocomplete on certain search inputs
By default, if the autocomplete feature is turned on, we will attach an Algolia dropdown to every search input of your website matching the jQuery selector input[name='s']
.
Note however that we also exclude inputs that have an associated no-autocomplete
class.
So to easily disable the Algolia autocomplete dropdown for a given search input, simply add the no-autocomplete
class to it.