Hooks Examples

Here is a list of different use cases that could be used to use our hooks.

Exclude a Custom Post Type from the translation

To avoid adding URLs to the Weglot exclusion list, you can disable the translation of a Custom Post Type (example: "book") directly with our hooks.

<?php
add_filter('weglot_is_eligible_url', 'not_authorize_translation');
function not_authorize_translation(){
if (get_post_type( weglot_get_postid_from_url() ) === 'book') {
return false;
}
return true;
}
add_filter('weglot_active_translation_before_process', 'check_page_translation');
function check_page_translation()
{
if (get_post_type(weglot_get_postid_from_url()) === 'book' && weglot_get_current_language() !== weglot_get_original_language() ) {
wp_redirect( weglot_get_request_url_service()->get_full_url_no_language() );
exit;
}
return true;
}

I advise you to redirect to avoid indexing the page in the translated language.

Add a data-attribute to be translated by Weglot

It can happen in your HTML that you use data-attributes that are not translated by default. Example with data-slide-title

<div data-slide-title="Title slide">
New project !
</div>

To do this, we use so-called "DOM Checkers". You must therefore first of all have the appropriate DOM Checker

<?php
use Weglot\Parser\Check\Dom\AbstractDomChecker;
use Weglot\Client\Api\Enum\WordType;
class Div_Slide_Title extends AbstractDomChecker
{
/**
* {@inheritdoc}
*/
const DOM = 'div';
/**
* {@inheritdoc}
*/
const PROPERTY = 'data-slide-title';
/**
* {@inheritdoc}
*/
const WORD_TYPE = WordType::TEXT;
}

Once you have your "DOM Checker" class, you must add it to our initial list with a filter:

<?php
add_filter('weglot_get_dom_checkers', 'weglot_add_dom_checkers');
function weglot_add_dom_checkers($dom_checkers)
{
$dom_checkers[] = '\Div_Slide_Title';
return $dom_checkers;
}

Add a JSON key to translate during an AJAX call

By default, Weglot only translates 2 keys during a JSON call: "post_title" and "rendered" ( apart from integrations and compatibility with different plugins like Contact Form 7)

However, you may need to translate additional keys. Let's take the example of a key: my_best_message

<?php
add_filter('weglot_keys_translate_json', 'custom_weglot_keys_translate_json');
function custom_weglot_keys_translate_json($keys){
$keys['/'] = [
'my_best_message',
];
return $keys;
}

Our key table works according to the URL that is called. The "/" will therefore react to all AJAX calls. However, if you have a specific AJAX call that targets a URL like: example.com/my-call/example, you could filter only on the "my-call" path:

<?php
add_filter('weglot_keys_translate_json', 'custom_weglot_keys_translate_json');
function custom_weglot_keys_translate_json($keys){
$keys['my-call'] = [
'my_best_message',
];
return $keys;
}

Our system is also recursive. No need to worry about the depth of your keys, we'll look at everything.

How to activate the translation of slugs for WooCommerce products?

<?php
add_filter('weglot_url_translate_metabox_post_type_exclude', 'active_metabox_translate_url');
function active_metabox_translate_url($excluded){
$index = array_search('product', $excluded);
if($index < 0){
return $excluded;
}
unset($excluded[$index]);
return $excluded;
}