WordPress

Last updated 5 months ago

Introduction

Weglot has built a powerful WordPress plugin that integrates in your WordPress website and make it multilingual in a few minutes.

What does Weglot plugin do exactly ?

  1. It creates URL for each languages, like /fr/about/ or /es/about/ for instance

  2. On these URL, it returns the translated content

  3. It adds a language button on the page and the hreflang tags in the <head> section of your page for SEO.

Getting started

To start, install Weglot Translate plugin directly from the directory.

In the settings page, configure the 3 mandatory settings :

  • API Key : You get an API key in your account. If you don't have one, you can create your account.

  • Original Language : The original language of your WordPress website.

  • Destination Languages : The languages you want your website to be translated into.

Save the settings and you are done. You will see the language button appear on your website.

Plugin settings

Override CSS

You can add your own CSS rules to customize the language button.

Weglot automatically adds a button in your page, that is either a dropdown or a list.

Dropdown
List
<aside data-wg-notranslate class="country-selector weglot-dropdown weglot-default weglot-invert">
<input id="weglot_choice" type="checkbox" name="menu">
<label for="weglot_choice" class="wgcurrent wg-li weglot-flags flag-0 en" data-code-language="en">
<span>EN</span>
</label>
<ul>
<li class="wg-li weglot-flags flag-0 fr" data-code-language="fr">
<a data-wg-notranslate href="http://example.com/">FR</a>
</li>
</ul>
</aside>
<aside data-wg-notranslate class="country-selector weglot-inline weglot-default weglot-invert">
<input id="weglot_choice" type="checkbox" name="menu">
<label for="weglot_choice" class="wgcurrent wg-li weglot-flags en" data-code-language="en">
<span>English</span>
</label>
<ul>
<li class="wg-li weglot-flags fr" data-code-language="fr">
<a data-wg-notranslate href="http://example.com/fr/">Français</a>
</li>
</ul>
</aside>

You can use these classes to ad your own rules. For example adding this in "Override CSS" :

.country-selector a {
text-transform: uppercase;
}

will make the language name uppercase

Exclude URL

By default, all pages are translated. You can use this field to enter URL that you don't want to translate. This field matches regex.

For example, if you want to exclude all URL that contains /blog/ , you can simply add "/blog/" in the option.

Another example, if you want to only translate your homepage, meaning you want to exclude every URL except / , you would enter the following : [^/]

Exclude blocks

By default, everything is translated inside a page. But sometimes, you don't want to translate part of your page. For example all product descriptions or a customer review area.

You can add any CSS selector in the field "Exclude blocks" and it will add an attribute data-wg-notranslate on this HTML element. Everything inside this element won't be translated.

For example, if you enter this : .product-description , then your translated page will be :

<h1 class="product-title">Mon super article</h1> <!--The title is being translated-->
<p class="product-description" data-wg-notranslate>My awesome article</p><!--The description is not being translated-->

WordPress filters

Weglot plugin is using WordPress filters to extend default capabilities

weglot_words_translate

Since : 1.12

You can add words that are present in your HTML page but not translated. It's useful when a word is not being translated by Weglot because it's inside a Javascript for example.

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_words_translate', 'words_translate');
function words_translate($words){
$words[] = "Monday";
$words[] = "Tuesday";
$words[] = "Nice to meet you";
return $words;
}

weglot_regex_amp

Since : 2.0

Default value : ([&\?/])amp(/)?$

You can modify the regex that validates if a URL is an AMP page

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_regex_amp', 'regex_amp');
function regex_amp($regex){
// Change regex $regex = 'other';
return $regex;
}

weglot_is_eligible_url

Since : 2.0

This hook allows you to influence whether or not you can translate a URL.

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_is_eligible_url', 'eligible_url');
function eligible_url( $bool ){
if( get_post_type( get_the_ID() ) === 'premium-post' ) {
return true;
}
return false;
}

weglot_url_auto_redirect

Since : 2.0

You can use this filter to change the automatic redirection URL

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_url_auto_redirect', 'auto_redirect');
function eligible_url( $url ){
return "https://example.com";
}

weglot_active_translation

Since : 2.0

This filter allows you to enable or disable translation

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_active_translation', 'active_translation');
function active_translation( $is_active ){
return false;
}

weglot_type_treat_page

Since : 2.0

This filter allows you to add a translation type. By default we manage :

  • html

  • json

This filter must be combined with : weglot_%s_treat_page

Example with type = xml

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_type_treat_page', 'type_treat_page');
function type_treat_page( $type ){
return 'xml';
}
functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_xml_treat_page', 'xml_treat_page');
function type_treat_page( $content ){
// You can modify the content as you wish
return $content;
}

weglot_href_lang

Since : 2.0

This filter allows you to modify the HTML generation of href lang links

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_href_lang', 'htm_href_lang');
function htm_href_lang( $content ){
// You can modify the content as you wish
return $content;
}

weglot_button_html

Since 2.0

This filter allows you to modify the HTML generation of weglot selector

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_button_html', 'button_html');
function button_html( $button_html ){
// You can modify the content as you wish
return $button_html;
}

weglot_get_dom_checkers

Since 2.0

This filter allows you to add a class to target an element of the DOM or attributes that are not translated

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_get_dom_checkers', 'get_dom_checkers');
function get_dom_checkers( $dom_checkers ){
$dom_checkers[] = '\Namespace\ClassDomChecker';
return $dom_checkers;
}

weglot_exclude_blocks

Since 2.0

This filter allows you to add blocks to exclude that cannot be modified from the settings.

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_exclude_blocks', 'exclude_blocks');
function exclude_blocks( $exclude_blocks ){
$exclude_blocks[] = '.description';
return $exclude_blocks;
}

weglot_exclude_urls

Since 2.0

This filter allows you to add urls to exclude that cannot be modified from the settings.

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_exclude_urls', 'exclude_urls');
function exclude_blocks( $exclude_urls ){
$exclude_urls[] = '/my-new-url';
return $exclude_urls;
}

weglot_css_custom_inline

Since 2.0

This filter allows you to add custom style by default without going through the settings.

functions.php
<?php
// File wp-content/themes/myTheme/functions.php​
add_filter('weglot_css_custom_inline', 'css_custom_inline');
function css_custom_inline( $css_custom ){
$css_custom .= '.country-selector { background-color: red; }';
return $css_custom;
}

Helper functions

Weglot plugin is exposing functions that can be called in your code.

To see the source code of all available functions, you can go to the file : weglot-functions.php (Only with 2.0)

getCurrentLanguage

Warning : No longer available in version 2

Use : weglot_get_current_language

getOriginalLanguage

Warning : No longer available in version 2

Use : weglot_get_original_language

getDestinationLanguage

Warning : No longer available in version 2

Use : weglot_get_destination_language

weglot_get_service

Since 2.0

Params :

  • $service (string)

Return : Object

This function allows you to retrieve a "Service" class from the plugin

<?php
$language_service = weglot_get_service( 'Language_Service_Weglot' );

weglot_get_options

Since 2.0

Return : string

This function allows you to retrieve all options Weglot

<?php
$options = weglot_get_options();
echo $options['original_language'];

weglot_get_option

Since 2.0

Params :

  • $key (string)

Return : string

This function allows you to retrieve a specific option Weglot

<?php
$original_language = weglot_get_options( 'original_language' );
echo $original_language;

weglot_get_current_language

Since 2.0

Return : string

Retrieves the current language of a page

<?php
$current_language = weglot_get_current_language();
echo $current_language;

weglot_get_original_language

Since 2.0

Return : string

<?php
$original_language = weglot_get_original_language();
echo $original_language;

weglot_get_destination_language

Since 2.0

Return : array

Retrieves destination languages option

<?php
$destination_language = weglot_get_destination_language();
echo $destination_language[0];

weglot_get_request_url_service

Since 2.0

Return : Request_Url_Service_Weglot

Retrieve Request URL Service Weglot

<?php
$request_url_service = weglot_get_request_url_service();

weglot_get_current_and_original_language

Since 2.0

Return : array

Recovers the original language and the common language

<?php
$current_and_original = weglot_get_current_and_original_language();
echo $current_and_original['original'];

weglot_get_languages_available

Since 2.0

Return : array

Recovers languages available from Weglot

<?php
$languages_available = weglot_get_languages_available();
echo $languages_available['fr']->getLocalName();

weglot_get_languages_configured

Since 2.0

Params :

  • $type (string|null) Default : null

Return : array

Get languages configured from options.

If type is null, you will retrieve an object. You can use "code" and retrieve only code language.

<?php
$languages_configured = weglot_get_languages_configured();
echo $languages_configured[0]->getLocalName(); // Example : 'Français'
// Or
$languages_configured = weglot_get_languages_configured('code');
echo $languages_configured[0]; // Example : 'fr'

weglot_get_button_selector_html

Since 2.0

Params :

  • $add_class (string)

Return : string

Generates the HTML of the Weglot selector.

<?php
$button_html = weglot_get_button_selector_html();
echo $button_html;

weglot_get_exclude_urls

Since 2.0

Return : array

Retrieves all URLs exclude

<?php
$exclude_urls = weglot_get_exclude_urls();
echo $exclude_urls[0];

weglot_get_translate_amp_translation

Since 2.0

Return : boolean

Enables you to see if translation of AMP pages is enabled

<?php
$amp_is_active = weglot_get_translate_amp_translation();
if($amp_is_active){ // ... }

weglot_get_current_full_url

Since 2.0

Return : string

Retrieves the current complete URL

<?php
$full_url = weglot_get_current_full_url();
echo $full_url;

weglot_is_eligible_url

Since 2.0

Return : boolean

Enables you to see if translation URL is possible

<?php
$is_eligible = weglot_is_eligible_url();
if($is_eligible){ // ... }

weglot_has_auto_redirect

Since 2.0

Return : boolean

This function allows to know if the auto-redirection is active or not

<?php
$has_auto_redirect = weglot_has_auto_redirect();
if($has_auto_redirect){ // ... }

How to add the selector after the menu?

If you want to have our HTML selector directly after your menu, you can use this code by adding it in your theme functions.php file

functions.php
// File wp-content/themes/myTheme/functions.php​
add_filter( 'wp_nav_menu_items', 'weglot_after_menu' );
function weglot_after_menu( $items ) {
$button = weglot_get_button_selector_html( '' );
$items .= $button;
return $items;
}