Setting Up Algolia
Creating a custom plugin
The plugin will contain the Algolia PHP API client and some basic setup, like a reindex command.
For the sake of simplicity we are not including files and details you would normally add when publishing a plugin to the WP marketplace.
Create an algolia-custom-integration
folder under the wp-content/plugins/
directory and create an algolia-custom-integration.php
file inside this new directory with the following content.
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.
Import Algolia PHP client
Without composer
Download the latest version of the Algolia API client via GitHub Release page.
Extract the downloaded zip inside the plugin directory and rename the top folder to api-client
.
Inside the api-client
folder, run the following command from your terminal:
1
php ./bin/install-dependencies-without-composer
It will create a new api-client/vendor/
directory with some required dependencies.
With composer
You can use composer in your plugin to directly pull the API client and its dependencies.
At the root of your plugin, execute the following command. It will create the composer.json
and composer.lock
files, and the vendor
directory containing all the dependencies.
These files and folder will be added to the root of your plugin (next to algolia-custom-integration.php
).
1
composer require algolia/algoliasearch-client-php
Instantiate the Algolia SearchClient
In the main plugin file algolia-custom-integration/algolia-custom-integration.php
we’ll
instantiate the Algolia client, and make it globally available.
1
2
3
4
5
6
7
require_once __DIR__ . '/api-client/autoload.php';
// If you use composer, require the composer autoload
// require_once __DIR__ . '/vendor/autoload.php';
global $algolia;
$algolia = \Algolia\AlgoliaSearch\SearchClient::create("YourApplicationID", "YourSearchOnlyApiKey");