Integrations / Platforms / WordPress / Setting Up Algolia

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.

Wp admin plugin activate

Importing the Algolia PHP client

With Composer

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.json and composer.lock files, as well as a vendor directory containing all the dependencies.

1
composer require algolia/algoliasearch-client-php

Without Composer

If you’re not using Composer, you can directly download the latest version of the Algolia API client on the GitHub release page.

Api client release download

Then, extract the archive inside the plugin directory and rename the top folder to api-client.

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");

Did you find this page helpful?