Weglot
WebsiteGithubSlack
  • Developer Documentation
  • WordPress
    • Getting Started
    • Helpers Functions
    • Hooks
      • Translations Hooks
      • Other Hooks
    • Use cases
      • Lang attribute
      • Translate link
      • Implementing Custom Link Translation
      • Wp-rocket Lazyload
      • Deactivate Weglot on Elementor
      • Weglot translate on Elementor
      • Language Selector Styling
      • Change flag size
      • Exclude a Custom Post Type
      • Exclude the search page
      • Exclude draft or private status post
      • Plugin Woocommerce: Translate all email sent to customers
      • Plugin WooCommerce: Translate Product URLs
      • Use custom language code (URL, lang and hreflang attribute)
      • Theme OceanWP: Use language selector in the main menu
      • Theme Avada: Use language selector in the main menu
      • LinkedIn Share Post: Translate oEmbed WP URL
      • Hide button on excluded page
      • Auto switch only for HomePage
      • Proxify Url
      • Translate pdf for Woocommerce pdf invoice
      • Translate pdf with Gravity pdf
      • Translate Dublin core meta (or other meta)
      • How to not translate specific ajax action
      • How to get translated url programmatically
      • How to set WP locale based on Weglot current language
      • How to translate mail from Photo Reviews for WooCommerce
      • How to translate schema from schema.org generate by Yoast plugin
      • How to translate email sending by plugin WP Mail SMTP
      • How to hide the Weglot admin button for non administrator roles
      • Exemple of snippet to translate search from JetSearch plugin
      • Example of snippet to translate your checkout element (override/added by the Funnel Builder plugin)
      • LScache
      • How to fix live builder issue with Weglot
      • Add translated url to Rankmath sitemap index
      • Advanced Woo Search X Weglot
      • Conditionally Disabling Weglot Translation for Specific URLs
    • Weglot filters
      • weglot_translate_email
      • weglot_cancel_init
      • weglot_translate_email_languages_forced
      • weglot_menu_parent_item_title
      • weglot_active_current_menu_item
      • weglot_ajax_no_translate
      • weglot_active_translation_before_process
      • weglot_active_translation_before_treat_page
      • weglot_debug_file
      • weglot_autoredirect_only_home
      • weglot_autoredirect_skip
      • weglot_remove_google_translate
      • weglot_add_hreflang
      • weglot_get_replace_modify_link
      • get_replace_modify_link_in_xml
      • weglot_get_flag_class
      • weglot_get_name_with_language_entry
      • weglot_get_class_dropdown
      • weglot_button_html
      • weglot_get_dom_checkers
      • weglot_replace_div_id
      • weglot_replace_weglot_menu
      • weglot_render_default_button
      • weglot_render_switcher_editor_button
      • weglot_href_lang
      • weglot_get_options_from_cdn_cache
      • weglot_get_options_from_cdn_cache_duration
      • weglot_get_slugs_from_cache
      • weglot_get_slugs_cache_duration
      • weglot_exclude_blocks
      • weglot_exclude_urls
      • weglot_get_parser_ignored_nodes
      • weglot_navigator_language
      • weglot_url_auto_redirect
      • weglot_replace_url
      • weglot_replace_link
      • weglot_ajax_replace_urls
      • weglot_proxify_urls
      • weglot_add_json_keys
      • weglot_json_treat_page
      • weglot_html_treat_page
      • Copy of weglot_xml_treat_page
      • weglot_render_dom
      • weglot_default_current_language_empty
    • Algolia integration
  • Proxy
    • Headers
  • Javascript
    • Getting Started
    • Options
    • Javascript functions
    • Language link Hooks
    • Translate iframe
    • Advanced concepts
      • Translation engines
  • CMS Specific
    • Shopify
  • API
    • Reference
  • Cookies
Powered by GitBook
On this page
  1. WordPress

Algolia integration

Integration between Algolia and the Weglot WordPress plugin.

Previousweglot_default_current_language_emptyNextHeaders

Last updated 11 months ago

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.

To install Algolia, please follow the steps described in this link: .

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

function algolia_load_assets() {
    $xhookPath = 'https://jpillora.com/xhook/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:

"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"
          ]
        }
      ]
    }
  ]
}

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!

Algolia WordPress Integration Setup