Setting Up Algolia
The first step of integrating Algolia in your WordPress instance is to create a custom plugin. The plugin takes care of interfacing with the PHP API client and contains the indexing logic.
For the sake of simplicity, this guide doesn’t include files and details you would typically add when publishing a plugin to the WP marketplace.
Creating a custom plugin
In your WordPress instance, add an
algolia-custom-integration folder under the
wp-content/plugins/ directory, and create an
algolia-custom-integration.php file. Then, paste the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 <?php /** * Plugin Name: Algolia Custom Integration * Description: Add Algolia Search feature * Text Domain: algolia-custom-integration * Version: 1.0.0 * * @package Algolia_Custom_Integration */ // Your code starts here.
Head to your WordPress admin dashboard and activate this new plugin.
Importing the Algolia PHP client
You can use Composer in your plugin to install the API client and its dependencies. In your terminal, navigate to the root of your plugin and execute the following command. It creates the
composer.lock files, as well as a
vendor directory containing all the dependencies.
1 composer require algolia/algoliasearch-client-php
Then, extract the archive inside the plugin directory and rename the top folder to
In your terminal, navigate to the
api-client folder and run the following command:
1 php ./bin/install-dependencies-without-composer
This creates a new
api-client/vendor/ directory with the required dependencies.
Instantiating the Algolia client
You can now instantiate the Algolia client in the main plugin file and make it globally available.
1 2 3 4 5 6 7 require_once __DIR__ . '/api-client/autoload.php'; // If you're using Composer, require the Composer autoload // require_once __DIR__ . '/vendor/autoload.php'; global $algolia; $algolia = \Algolia\AlgoliaSearch\SearchClient::create("YourApplicationID", "YourAdminApiKey");