WordPress

Last updated 7 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;
}

File : weglot/src/actions/admin/class-customize-menu-weglot.php (Line: 79)

Default argument : 'weglot-hide'

No argument

File : weglot/src/actions/admin/class-customize-menu-weglot.php (Line: 90)

Default argument : $flag_class .$lang[0]

No argument

weglot_custom_nav_menu_items

File : weglot/src/actions/admin/class-customize-menu-weglot.php (Line: 142)

Default argument : $languages_configured

No argument

weglot_url_translate_metabox_post_type_exclude

File : weglot/src/actions/admin/class-metabox-url-translate-weglot.php (Line: 212)

Default argument : ['attachment','seopress_404',]

No argument

weglot_query_vars_check

File : weglot/src/actions/front/class-search-weglot.php (Line: 43)

Default argument : 's'

No argument

weglot_ajax_no_translate

File : weglot/src/actions/front/class-translate-page-weglot.php (Line: 80)

Default argument : ['add-menu-item','query-attachments','avia_ajax_switch_menu_walker','query-themes','wpestate_ajax_check_booking_valability_internal','mailster_get_template',]

No argument

weglot_active_translation_before_process

File : weglot/src/actions/front/class-translate-page-weglot.php (Line: 127)

Default argument : true

No argument

weglot_active_translation_before_treat_page

File : weglot/src/actions/front/class-translate-page-weglot.php (Line: 143)

Default argument : true

No argument

weglot_debug_file

File : weglot/src/actions/front/class-translate-page-weglot.php (Line: 149)

Default argument : WEGLOT_DIR . '/content.html'

No argument

weglot_get_clean_base_url

File : weglot/src/helpers/class-helper-filter-url-weglot.php

Default argument : $url

No argument

File : weglot/src/helpers/class-helper-replace-url-weglot.php

Default argument : $data

No argument

weglot_get_flag_class

File : weglot/src/services/class-button-service-weglot.php

Default argument : $flag_class

No argument

weglot_get_name_with_language_entry

File : weglot/src/services/class-button-service-weglot.php

Default argument : $name

Arguments :

  • $language_entry

weglot_get_class_dropdown

File : weglot/src/services/class-button-service-weglot.php

Default argument : $class

No argument

weglot_view_button_html

File : weglot/src/services/class-button-service-weglot.php (Line: 104)

Default argument : $view_button

No argument

weglot_condition_test_custom_url

File : weglot/src/services/class-custom-url-service-weglot.php (Line: 40)

Default argument : $condition_test_custom_url

No argument

File : weglot/src/services/class-custom-url-service-weglot.php (Line: 57)

Default argument : $url_lang

Arguments :

  • $key_code

File : weglot/src/services/class-custom-url-service-weglot.php

Default argument : $link_button

No argument

File : weglot/src/services/class-custom-url-service-weglot.php

Default argument : $link_button

No argument

weglot_get_dom_listeners

File : weglot/src/services/class-dom-listeners-service-weglot.php

Default argument : $this->dom_listeners

No argument

weglot_replace_div_id

File : weglot/src/services/class-generate-switcher-service-weglot.php

Default argument : $dom

No argument

weglot_replace_weglot_menu

File : weglot/src/services/class-generate-switcher-service-weglot.php

Default argument : $dom

No argument

weglot_render_default_button

File : weglot/src/services/class-generate-switcher-service-weglot.php

Default argument : $dom

No argument

weglot_generate_switcher_from_dom

File : weglot/src/services/class-generate-switcher-service-weglot.php

Default argument : $dom

No argument

weglot_current_language_entry

File : weglot/src/services/class-language-service-weglot.php (Line: 121)

Default argument : $key_code

No argument

weglot_get_options

File : weglot/src/services/class-option-service-weglot.php

Default argument : wp_parse_args(get_option(WEGLOT_SLUG),$this->get_options_default())

No argument

weglot_destination_languages

File : weglot/src/services/class-option-service-weglot.php

Default argument : $destination_languages

No argument

weglot_flag_css

File : weglot/src/services/class-option-service-weglot.php

Default argument : $this->get_option('flag_css')

No argument

weglot_other_words_unformat_from_api

File : weglot/src/services/class-other-translate-service-weglot.php (Line: 70)

Default argument : true

No argument

weglot_parser_config_provider

File : weglot/src/services/class-parser-service-weglot.php (Line: 56)

Default argument : class ServerConfigProvider

No argument

weglot_get_parser_ignored_nodes

File : weglot/src/services/class-parser-service-weglot.php (Line: 68)

Default argument : $parser->getIgnoredNodesFormatter()->getIgnoredNodes()

No argument

weglot_redirection_language_exception

File : weglot/src/services/class-redirect-service-weglot.php

Default argument : $server_lang

No argument

weglot_no_replace_url_condition

File : weglot/src/services/class-replace-link-service-weglot.php (Line: 29)

Default argument : 'wp-content/uploads'

No argument

weglot_no_replace_a_href

File : weglot/src/services/class-replace-link-service-weglot.php (Line: 96)

Default argument : 'wp-content/uploads'

No argument

File : weglot/src/services/class-replace-url-service-weglot.php

Default argument : $dom

No argument

weglot_length_replace_a

File : weglot/src/services/class-replace-url-service-weglot.php (Line: 65)

Default argument : 1500

No argument

File : weglot/src/services/class-replace-url-service-weglot.php (Line: 74)

Default argument : 'replace_' . $type

Arguments :

  • $type

File : weglot/src/services/class-replace-url-service-weglot.php (Line: 105)

Default argument : $_SERVER['HTTP_HOST']

No argument

weglot_translate_current_language

File : weglot/src/services/class-request-url-service-weglot.php

Default argument : $current_language

No argument

weglot_json_treat_page

File : weglot/src/services/class-translate-service-weglot.php (Line: 98)

Default argument : $content

No argument

weglot_html_treat_page

File : weglot/src/services/class-translate-service-weglot.php (Line: 112)

Default argument : $translated_content

No argument

$name_filter

File : weglot/src/services/class-translate-service-weglot.php

Default argument : $content

Arguments :

  • $parser

  • $this->original_language

  • $this->current_language

weglot_array_not_ajax_html

File : weglot/src/services/class-translate-service-weglot.php (Line: 143)

Default argument : ['redirecturl','url',]

No argument

weglot_array_not_ajax_html

File : weglot/src/services/class-translate-service-weglot.php (Line: 172)

Default argument : ['redirecturl','url',]

No argument

weglot_is_ajax_html_regex

File : weglot/src/services/class-translate-service-weglot.php (Line: 194)

Default argument : '/<(a|div|span|p|i|aside|input|textarea|select|h1|h2|h3|h4|meta|button|form|li|strong|ul|option)/'

No argument

weglot_render_dom

File : weglot/src/services/class-translate-service-weglot.php

Default argument : $dom

No argument

widget_title

File : weglot/src/widgets/class-widget-selector-weglot.php (Line: 36)

Default argument : $instance['title']

No argument

wp_doing_ajax

File : weglot/weglot-compatibility.php

Default argument : defined('DOING_AJAX') && DOING_AJAX

No argument

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;
}
Contents
IntroductionGetting startedPlugin settingsWordPress filtersweglot_words_translateweglot_regex_ampweglot_is_eligible_urlweglot_url_auto_redirectweglot_active_translationweglot_type_treat_pageweglot_href_langweglot_button_htmlweglot_get_dom_checkersweglot_exclude_blocksweglot_exclude_urlsweglot_css_custom_inlineweglot_nav_menu_link_classweglot_nav_menu_link_classweglot_custom_nav_menu_itemsweglot_url_translate_metabox_post_type_excludeweglot_query_vars_checkweglot_ajax_no_translateweglot_active_translation_before_processweglot_active_translation_before_treat_pageweglot_debug_fileweglot_get_clean_base_urlweglot_get_replace_modify_linkweglot_get_flag_classweglot_get_name_with_language_entryweglot_get_class_dropdownweglot_view_button_htmlweglot_condition_test_custom_urlweglot_link_languageweglot_get_link_with_key_codeweglot_get_link_button_with_key_codeweglot_get_dom_listenersweglot_replace_div_idweglot_replace_weglot_menuweglot_render_default_buttonweglot_generate_switcher_from_domweglot_current_language_entryweglot_get_optionsweglot_destination_languagesweglot_flag_cssweglot_other_words_unformat_from_apiweglot_parser_config_providerweglot_get_parser_ignored_nodesweglot_redirection_language_exceptionweglot_no_replace_url_conditionweglot_no_replace_a_hrefweglot_replace_linkweglot_length_replace_aweglot_modify_link_replace_functionweglot_check_link_server_hostweglot_translate_current_languageweglot_json_treat_pageweglot_html_treat_page$name_filterweglot_array_not_ajax_htmlweglot_array_not_ajax_htmlweglot_is_ajax_html_regexweglot_render_domwidget_titlewp_doing_ajaxHelper functionsweglot_get_serviceweglot_get_optionsweglot_get_optionweglot_get_current_languageweglot_get_original_languageweglot_get_destination_languageweglot_get_request_url_serviceweglot_get_current_and_original_languageweglot_get_languages_availableweglot_get_button_selector_htmlweglot_get_exclude_urlsweglot_get_translate_amp_translationweglot_get_current_full_urlweglot_is_eligible_urlweglot_has_auto_redirectHow to add the selector after the menu?