Last updated on October 18, 2023

WPML language switcher RTL

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Enable RTL language support in WPML language switcher.

The WPML plugin is a popular choice for creating multilingual websites with WordPress. One common requirement for multilingual websites is to have a language switcher that allows users to switch between different language versions of the site. In some cases, you may also need to support right-to-left (RTL) languages, such as Arabic or Hebrew.

To add an RTL language switcher using WPML, you can use the icl_get_languages() function to retrieve the list of available languages and their details. Then, you can loop through the languages and output the language switcher HTML markup with appropriate RTL classes.

Here’s an example code snippet that demonstrates how to create an RTL language switcher using WPML:

function wpsnippets_rtl_language_switcher() {
    $languages = icl_get_languages();

    if (count($languages) > 1) {
        echo '<ul class="language-switcher">';

        foreach ($languages as $language) {
            $language_code = $language['language_code'];
            $language_name = $language['native_name'];
            $language_url = $language['url'];

            $rtl_class = $language['rtl'] ? 'rtl' : '';

            echo '<li class="' . $rtl_class . '">';
            echo '<a href="' . $language_url . '">' . $language_name . '</a>';
            echo '</li>';
        }

        echo '</ul>';
    }
}

To use this code snippet, you can simply call the wpsnippets_rtl_language_switcher() function in your theme or plugin template file where you want to display the language switcher. The function will output an unordered list (<ul>) with each language as a list item (<li>), and the language name as a link (<a>) to switch to that language version of the site. The rtl class will be added to the list item if the language is RTL.

You can customize the HTML markup and CSS classes according to your specific design requirements.

Examples

Example 1: Adding WPML language switcher with RTL support to the header

This use case demonstrates how to add a WPML language switcher to the header of a WordPress website, with support for right-to-left (RTL) languages. The code example below adds the language switcher to the header template file (header.php) using the wpml_language_switcher() function.

<?php if (function_exists('wpml_language_switcher')) : ?>
    <div class="language-switcher">
        <?php wpml_language_switcher(array('dropdown' => 0, 'show_flags' => 0, 'show_names' => 1, 'hide_if_empty' => 0, 'hide_current' => 0)); ?>
    </div>
<?php endif; ?>

Explanation: The code checks if the wpml_language_switcher() function exists before adding the language switcher to the header. The function is then called with an array of arguments to customize the display of the language switcher. In this example, the dropdown is disabled (dropdown => 0), flags are hidden (show_flags => 0), language names are shown (show_names => 1), and empty languages are not hidden (hide_if_empty => 0). The language switcher is wrapped in a <div> element with the class language-switcher for styling purposes.

Example 2: Adding WPML language switcher with RTL support to a sidebar widget

This use case demonstrates how to add a WPML language switcher to a sidebar widget, with support for right-to-left (RTL) languages. The code example below adds the language switcher to a sidebar widget using the wpml_language_switcher() function.

<?php if (function_exists('wpml_language_switcher')) : ?>
    <div class="widget">
        <h3 class="widget-title">Language</h3>
        <div class="widget-content">
            <?php wpml_language_switcher(array('dropdown' => 1, 'show_flags' => 1, 'show_names' => 0, 'hide_if_empty' => 1, 'hide_current' => 1)); ?>
        </div>
    </div>
<?php endif; ?>

Explanation: Similar to the previous example, the code checks if the wpml_language_switcher() function exists before adding the language switcher to the sidebar widget. The function is then called with different arguments to customize the display of the language switcher. In this example, the dropdown is enabled (dropdown => 1), flags are shown (show_flags => 1), language names are hidden (show_names => 0), and empty languages are hidden (hide_if_empty => 1). The language switcher is wrapped in a <div> element with the class widget for styling purposes.

Example 3: Adding WPML language switcher with RTL support to the footer

This use case demonstrates how to add a WPML language switcher to the footer of a WordPress website, with support for right-to-left (RTL) languages. The code example below adds the language switcher to the footer template file (footer.php) using the wpml_language_switcher() function.

<?php if (function_exists('wpml_language_switcher')) : ?>
    <div class="language-switcher">
        <?php wpml_language_switcher(array('dropdown' => 0, 'show_flags' => 1, 'show_names' => 1, 'hide_if_empty' => 0, 'hide_current' => 0)); ?>
    </div>
<?php endif; ?>

Explanation: Similar to the first example, the code checks if the wpml_language_switcher() function exists before adding the language switcher to the footer. The function is then called with the same arguments as the first example to customize the display of the language switcher. In this example, the dropdown is disabled (dropdown => 0), flags are shown (show_flags => 1), language names are shown (show_names => 1), and empty languages are not hidden (hide_if_empty => 0). The language switcher is wrapped in a <div> element with the class language-switcher for styling purposes.

Last updated on October 18, 2023. Originally posted on January 3, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *