# Algolia integration

Algolia is a powerful search and discovery API platform designed to create fast and relevant search experiences for websites and applications. It provides tools to index, search, and analyze data in real-time, ensuring that users can quickly find what they're looking for.<br>

To install Algolia, please follow the steps described in this link: [Algolia WordPress Integration Setup](https://www.algolia.com/doc/integration/wordpress/indexing/setting-up-algolia/?client=php).<br>

To make Algolia work with Weglot, you need to add the following code to a snippet or directly into your user code:

```php
function algolia_load_assets() {
    $xhookPath = 'https://unpkg.com/xhook@1.6.2/dist/xhook.min.js';
    wp_enqueue_script('xhook', $xhookPath, array(), '1.0.0', true);
    $algoliaPath = plugins_url().'/weglot/app/javascripts/algolia.js';
    $algoliaVersion = date("ymd-Gis", filemtime(get_template_directory() . $algoliaPath));
    wp_enqueue_script('wg-algolia-js', $algoliaPath, array(), $algoliaVersion, true);
    $api_key = weglot_get_option( 'api_key' );
    wp_localize_script('wg-algolia-js', 'weglotData', array(
        'api_key' => $api_key,
        'original_language' => weglot_get_original_language(),
        'current_language' => weglot_get_current_language()
    ));
}
add_action('wp_enqueue_scripts', 'algolia_load_assets');

```

This code adds the required JavaScript file for Algolia and includes the xhook library to intercept and proxy Algolia requests. The `wp_enqueue_script` function is used to load the xhook library and the custom Algolia JavaScript file. Additionally, the `wp_localize_script` function passes Weglot configuration data (such as the API key, original language, and current language) to the custom JavaScript file.\
\
If everything is set up correctly, you should see the Algolia JavaScript file from Weglot and the xhook library loaded on your page.

To complete the setup, you now need to add some definitions to your Weglot custom settings to specify what content you want to translate. You can do this by including the following code:

```json
"definitions": {
  "translations": [
    {
      "type": "JSON",
      "value": [
        {
          "paths": [
            "$.results[*].hits[*].post_title",
            "$.results[*].hits[*].content",
            "$.results[*].hits[*]._snippetResult.post_title.value",
            "$.results[*].hits[*]._snippetResult.content.value",
            "$.hits[*].post_title",
            "$.hits[*].content",
            "$.hits[*]._snippetResult.post_title.value",
            "$.hits[*]._snippetResult.content.value"
          ]
        }
      ]
    }
  ]
}
```

<figure><img src="/files/MuhUlGlEmTIcuera8ATj" alt=""><figcaption></figcaption></figure>

Please note that this integration is still a Work In Progress (WIP), and we greatly appreciate any feedback you may have. Your insights will help us improve the integration and ensure a seamless experience. Thank you for your support!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.weglot.com/wordpress/algolia-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
