Last updated on October 18, 2023

WPML language switcher widget

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

Add a language switcher widget with WPML.

The WPML language switcher widget is a useful tool for websites that are multilingual and use the WPML plugin. This widget allows users to easily switch between different languages on your website.

To add the WPML language switcher widget to your WordPress site, you can use the following code:

function wpsnippets_add_wpml_language_switcher_widget() {
    if ( function_exists( 'icl_get_languages' ) ) {
        $languages = icl_get_languages( 'skip_missing=0' );
        if ( ! empty( $languages ) ) {
            echo '<ul class="wpml-language-switcher">';
            foreach ( $languages as $language ) {
                echo '<li>';
                if ( $language['active'] ) {
                    echo '<span class="active">';
                }
                echo '<a href="' . esc_url( $language['url'] ) . '">' . esc_html( $language['native_name'] ) . '</a>';
                if ( $language['active'] ) {
                    echo '</span>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }
}
add_action( 'wpml_add_language_selector', 'wpsnippets_add_wpml_language_switcher_widget' );

This code adds a custom function wpsnippets_add_wpml_language_switcher_widget() that checks if the WPML plugin is active and retrieves the list of languages using the icl_get_languages() function. It then loops through each language and outputs a list item with a link to switch to that language. The active language is highlighted with a CSS class.

To display the language switcher widget, you need to add the following code to your theme’s template file or widget area:

do_action( 'wpml_add_language_selector' );

This code uses the do_action() function to trigger the wpml_add_language_selector hook, which in turn calls our custom function to display the language switcher widget.

You can customize the appearance of the language switcher widget by modifying the HTML and CSS in the code snippet.

Examples

Example 1: Adding WPML Language Switcher Widget to a Sidebar

This example demonstrates how to add the WPML Language Switcher widget to a sidebar in WordPress.

<?php
function wpsnippets_add_wpml_language_switcher_widget() {
    if ( function_exists( 'icl_get_languages' ) ) {
        the_widget( 'SitePress_Widgets_Languages', array( 'dropdown' => 0 ) );
    }
}
add_action( 'wp_loaded', 'wpsnippets_add_wpml_language_switcher_widget' );
?>

In this example, we use the the_widget() function to add the WPML Language Switcher widget to a sidebar. The icl_get_languages() function is used to check if WPML is active before adding the widget. By setting the 'dropdown' parameter to 0, we disable the dropdown option for the language switcher.

Example 2: Displaying WPML Language Switcher Widget in a Template File

This example demonstrates how to display the WPML Language Switcher widget in a template file in WordPress.

<?php
if ( function_exists( 'icl_get_languages' ) ) {
    the_widget( 'SitePress_Widgets_Languages', array( 'dropdown' => 1 ) );
}
?>

In this example, we use the the_widget() function to display the WPML Language Switcher widget directly in a template file. The icl_get_languages() function is used to check if WPML is active before displaying the widget. By setting the 'dropdown' parameter to 1, we enable the dropdown option for the language switcher.

Example 3: Customizing WPML Language Switcher Widget Output

This example demonstrates how to customize the output of the WPML Language Switcher widget in WordPress.

<?php
function wpsnippets_customize_wpml_language_switcher_widget( $output, $args ) {
    // Modify the $output variable as needed
    return $output;
}
add_filter( 'wpml_widget_languages_dropdown', 'wpsnippets_customize_wpml_language_switcher_widget', 10, 2 );
?>

In this example, we use the wpml_widget_languages_dropdown filter to customize the output of the WPML Language Switcher widget. The wpsnippets_customize_wpml_language_switcher_widget function is hooked into the filter and receives the $output and $args parameters. You can modify the $output variable within the function to customize the widget’s output.

Last updated on October 18, 2023. Originally posted on December 7, 2023.

Leave a Reply

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