Skip to content

Instantly share code, notes, and snippets.

@kjtolsma
Last active April 24, 2026 12:33
Show Gist options
  • Select an option

  • Save kjtolsma/600d1159b6dee96b255f50a11ca2c1a1 to your computer and use it in GitHub Desktop.

Select an option

Save kjtolsma/600d1159b6dee96b255f50a11ca2c1a1 to your computer and use it in GitHub Desktop.
Weglot
<?php
/**
* Weglot
*/
class Weglot {
/**
* Construct
*/
public function __construct() {
/**
* Weglot dynamic translation for Algolia search results.
*/
\add_filter( 'weglot_translate_dynamics', '__return_true' );
\add_filter( 'weglot_allowed_urls', [ $this, 'weglot_allowed_urls' ] );
\add_filter( 'weglot_dynamics_selectors', [ $this, 'weglot_dynamics_selectors' ] );
\add_filter( 'weglot_whitelist_selectors', [ $this, 'weglot_dynamics_selectors' ] );
}
/**
* Weglot allowed URLs for Algolia search results.
*
* @param array $urls Default Weglot allowed URLs.
*/
public function weglot_allowed_urls( $urls ) {
return 'all';
}
/**
* Weglot dynamic translation for Algolia search results.
*
* @param array $default_dynamics Default Weglot dynamic selectors.
*/
public function weglot_dynamics_selectors( $default_dynamics ) {
return [
[
'value' => '.algolia-search',
],
];
}
}
/**
* Scripts
*/
class Scripts {
/**
* Construct
*/
public function __construct() {
\add_action( 'wp_enqueue_scripts', [ $this, 'algolia_load_assets' ] );
}
/**
* Temp Algolia
*/
public 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()
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment