Last updated on October 18, 2023

WPML language switcher flags customization

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

Customize flags in WPML language switcher.

The WPML plugin is a popular choice for creating multilingual websites with WordPress. One of the features it provides is a language switcher that allows visitors to switch between different languages on your site. By default, the language switcher displays the language names as text. However, you may want to customize it by using flags instead.

To achieve this, you can use the icl_get_languages() function provided by WPML to retrieve the list of languages and their corresponding information. Then, you can loop through the languages and output the flags using HTML and CSS.

Here’s an example code snippet that demonstrates how to customize the WPML language switcher with flags:

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

    if ( ! empty( $languages ) ) {
        foreach ( $languages as $language ) {
            $language_code = $language['language_code'];
            $country_flag_url = $language['country_flag_url'];

            echo '<a href="' . esc_url( $language['url'] ) . '">';
            echo '<img src="' . esc_url( $country_flag_url ) . '" alt="' . esc_attr( $language_code ) . '">';
            echo '</a>';
        }
    }
}

In this code snippet, we define a custom function called wpsnippets_custom_language_switcher_flags(). Inside the function, we first retrieve the list of languages using the icl_get_languages() function and store it in the $languages variable.

Next, we check if the $languages array is not empty. If it’s not empty, we loop through each language using a foreach loop. Inside the loop, we extract the language code and country flag URL from the language array.

Then, we output an anchor tag (<a>) with the language URL as the href attribute. Inside the anchor tag, we output an image tag (<img>) with the country flag URL as the src attribute and the language code as the alt attribute.

You can place this code snippet in your theme’s functions.php file or in a custom plugin. After adding the code, you can use the wpsnippets_custom_language_switcher_flags() function wherever you want to display the customized language switcher with flags.

Note: This code assumes that you have already set up the WPML plugin and configured the languages and translations for your site.

Examples

Example 1: Customizing WPML language switcher flags using CSS

This example demonstrates how to customize the flags displayed in the WPML language switcher by adding custom CSS.

/* Custom CSS to change the flag icons */
.wpsnippets_wpml-language-switcher img {
    /* Add your custom CSS here */
}

In this code example, you can add your own CSS rules inside the .wpsnippets_wpml-language-switcher img selector to customize the appearance of the flag icons in the WPML language switcher.

Example 2: Customizing WPML language switcher flags using a custom function

This example demonstrates how to customize the flags displayed in the WPML language switcher by using a custom PHP function.

function wpsnippets_custom_wpml_flags( $flags ) {
    // Add your custom code here to modify the $flags array
    return $flags;
}
add_filter( 'wpml_flags_filter', 'wpsnippets_custom_wpml_flags' );

In this code example, you can modify the $flags array inside the wpsnippets_custom_wpml_flags function to customize the flags displayed in the WPML language switcher. You can add, remove, or modify the flag icons by manipulating the array elements.

Example 3: Customizing WPML language switcher flags using a custom template

This example demonstrates how to customize the flags displayed in the WPML language switcher by creating a custom template.

function wpsnippets_custom_wpml_flags_template( $template ) {
    // Add your custom code here to load a custom template file
    return $template;
}
add_filter( 'icl_template_filter', 'wpsnippets_custom_wpml_flags_template' );

In this code example, you can modify the $template variable inside the wpsnippets_custom_wpml_flags_template function to load a custom template file for the WPML language switcher. By creating a custom template, you have full control over the HTML markup and can customize the flags as needed.

Last updated on October 18, 2023. Originally posted on October 25, 2023.

Leave a Reply