https://github.com/upstatement/algolia-connector-wp-plugin
A WordPress plugin for Algolia search with multisite support
https://github.com/upstatement/algolia-connector-wp-plugin
algolia php wordpress wordpress-plugin
Last synced: 5 months ago
JSON representation
A WordPress plugin for Algolia search with multisite support
- Host: GitHub
- URL: https://github.com/upstatement/algolia-connector-wp-plugin
- Owner: Upstatement
- License: mit
- Created: 2020-03-19T13:28:18.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-06-11T17:06:48.000Z (9 months ago)
- Last Synced: 2025-06-11T19:04:47.006Z (9 months ago)
- Topics: algolia, php, wordpress, wordpress-plugin
- Language: PHP
- Homepage:
- Size: 109 KB
- Stars: 0
- Watchers: 8
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Algolia Connector WordPress plugin
Hooks up a connection from WordPress to Algolia.
> [!WARNING]
> Note that this plugin is in active development and may have missing features or bugs. It is intended primarily as a starting point for a WordPress to Algolia integration. Use with caution!
## System Requirements
- PHP 8.2 or newer.
- WordPress version 6.6 or newer.
- [Composer](https://getcomposer.org/)
## Installation
1. Download this plugin and add it to the `/wp-content/plugins/` directory of your WordPress environment.
2. Install PHP dependencies by running `composer install` in the plugin directory.
## Usage
Once you have set up your Algolia application, retrieve your account credentials in the [API Keys](https://www.algolia.com/account/api-keys/all) section of your Algolia account. This plugin provides a settings page that can be used to store these values, but it is recommended that you use environment variables (you can also save the values as constants). The following
| Value | Variable name |
| -------------- | ------------------------ |
| Application ID | `ALGOLIA_APPLICATION_ID` |
| Search API key | `ALGOLIA_SEARCH_API_KEY` |
| Admin API key | `ALGOLIA_ADMIN_API_KEY` |
| Index name | `ALGOLIA_INDEX_NAME` |
## Configuring your theme
The plugin exposes a few hooks that allow you to customize what and how data gets sent to the Algolia index.
### `do_action( "algolia_indexable_post_types", array $post_types )`
By default, no content types will be indexed. You can add which post types should be indexed by returning them from the `algolia_indexable_post_types` filter:
#### Parameters
- `$post_types` array
The list of post types that can be indexed.
#### Example
```php
add_filter( 'algolia_indexable_post_types', function( $post_types ) {
$indexable_types = array( 'post', 'page', 'some_custom_type' );
return array_merge( $post_types, $indexable_types );
} );
```
### `do_action( "algolia_{$post->post_type}_to_record", array $record )`
You can customize the Algolia record with the `algolia_{$post->post_type}_to_record` hook, replacing the dynamic portion with the post type handle.
#### Parameters
- `$record` array
The record object that will be saved to Algolia. By default, the record is saved with the following values:
- `distinct_key` A unique identifier for the post object within Algolia.
- `post_id` ID of the post.
- `title` Title of the post.
- `post_type` Type of the post.
- `url` A URL for the post, retrieved with `get_permalink()`.
- `book` A default boost setting.
- `content` Content. This is usually one of the searchable fields.
```php
// Modify the record for all Page posts.
add_filter( 'algolia_page_to_record', function ( $record ) {
// You can access the WP_Post object like this:
$_post = get_post( $record['post_id'] );
// Add other Page data, like post metadata, to the record.
$record['date'] = get_post_meta( $record['post_id'], 'some_meta_key', true );
return $record;
} );
```
## WP CLI
This plugin comes with a few WP CLI commands that can be used to manage an index.
```shell
# Reindex everything.
wp algolia reindex
# Reindex just posts and pages.
wp algolia reindex --type=post,page
# Reindex a single post.
wp algolia reindex_single 123
# Delete all records in an index.
wp algolia clear
# Test the connection.
wp algolia test_connection
```