Language Selector Styling

Adding your own CSS code or HTML button

Override language selector 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 and will look like this:

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 add your own rules. For example adding this in "Override CSS" to make the language name uppercase. :

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

Override CSS on the WordPress back office

You can add your custom CSS rules directly on your WordPress back office. To do that, connect you on your WordPress and go to: Weglot > Language button design (Optional) > Override CSS

Override CSS using WordPress Hook

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

<?php
add_filter( 'weglot_css_custom_inline', 'custom_weglot_css_custom_inline' );
function custom_weglot_css_custom_inline( $css_custom ){
$css_custom .= '.country-selector { background-color: red; }';
return $css_custom;
}

Don't load Weglot CSS file

By default, the Weglot plugin loads the stylesheet /wp-content/plugins/weglot/dist/css/front-css.css. If you would prefer not to load the Weglot stylesheet, you can use the WordPress wp_enqueue_scripts filter as such:

<?php
add_action( 'wp_enqueue_scripts', 'custom_weglot_dequeue_style' );
function custom_weglot_dequeue_style() {
wp_dequeue_style( 'weglot-css' );
}

Override language selector HTML Markup

The Weglot WordPress plugin contains a filter that allows you to modify the HTML structure of your language-switch button.

Here's where you can find a description of this filter in the complete documentation of Weglot WordPress hooks: https://developers.weglot.com/wordpress/filters/other-filters#hook-weglot_button_html

Hook parameters:

  • $button_html: String containing the switcher's HTML code before it is shown live.

  • $add_class: String containing the classes, separated by spaces, related to the switcher button's layout context and customizable options.

Example: Adding code before/after the button

The example below allows you to easily add HTML code before and/or after the button or use preg_replace()function on the selector HTML content.

<?php
add_filter( 'weglot_button_html', 'custom_weglot_button_html', 10, 2 );
function custom_weglot_button_html( $button_html, $add_class ) {
$button_html = '<div style="border: 1px solid red;"><p>Choose your language:</p>' . $button_html . '</div>';
return $button_html;
}

Example: Rebuild the language selector from scratch

If you'd like to make major changes to the HTML structure of your language button, you can start with the code below.

<?php
add_filter( 'weglot_button_html', 'custom_weglot_button_html' );
function custom_weglot_button_html( $html , $add_class = '' ) {
$current_this = weglot_get_service('Button_Service_Weglot');
$private_mode = $current_this->option_services->get_option( 'private_mode' );
$original_language = weglot_get_original_language();
$private_all_languages = $current_this->private_language_services->private_mode_for_all_languages();
if ( apply_filters( 'weglot_view_button_html', $private_all_languages ) ) {
return '';
}
$weglot_url = $current_this->request_url_services->get_weglot_url();
$amp_regex = $current_this->amp_services->get_regex( true );
$destination_language = weglot_get_destination_languages();
$current_language = $current_this->request_url_services->get_current_language( false );
if ( weglot_get_translate_amp_translation() && preg_match( '#' . $amp_regex . '#', $weglot_url->getUrl() ) === 1 ) {
$add_class .= ' weglot-invert';
}
$flag_class = $current_this->get_flag_class();
$class_aside = $current_this->get_class_dropdown();
$button_html = sprintf( '<!--Weglot %s-->', WEGLOT_VERSION );
$button_html .= sprintf( "<aside data-wg-notranslate class='country-selector %s'>", $class_aside . $add_class );
if ( ! empty( $original_language ) && ! empty( $destination_language ) ) {
$current_language_entry = $current_this->language_services->get_current_language_entry_from_key( $current_language );
$name = $current_this->get_name_with_language_entry( $current_language_entry );
$uniq_id = 'wg' . uniqid( strtotime( 'now' ) ) . rand( 1, 1000 );
$button_html .= sprintf( '<input id="%s" class="weglot_choice" type="checkbox" name="menu"/><label for="%s" class="wgcurrent wg-li weglot-lang weglot-language %s" data-code-language="%s"><span>%s</span></label>', $uniq_id, $uniq_id, $flag_class . $current_language, $current_language_entry->getIso639(), $name );
$button_html .= '<ul>';
array_unshift( $destination_language, $original_language );
foreach ( $destination_language as $key => $key_code ) {
if ( $key_code === $current_language || $current_this->private_language_services->is_active_private_mode_for_lang( $key_code ) ) {
continue;
}
$button_html .= sprintf( '<li class="wg-li weglot-lang weglot-language %s" data-code-language="%s">', $flag_class . $key_code, $key_code );
$current_language_entry = $current_this->language_services->get_current_language_entry_from_key( $key_code );
$name = $current_this->get_name_with_language_entry( $current_language_entry );
$link_button = $current_this->custom_url_services->get_link_button_with_key_code( $key_code );
$button_html .= sprintf(
'<a data-wg-notranslate href="%s">%s</a>',
$link_button,
$name
);
$button_html .= '</li>';
}
$button_html .= '</ul>';
}
$button_html .= '</aside>';
return $button_html;
}