Last updated on October 18, 2023

WPML language switcher in sidebar

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

Add WPML language switcher to your sidebar.

To add a WPML language switcher in the sidebar of your WordPress website, you can use the wp_nav_menu() function along with some custom code. This will allow you to display a list of available languages and their corresponding links in the sidebar.

Here’s an example code snippet that you can use:

function wpsnippets_wpml_language_switcher() {
    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 ) {
                if ( ! $language['active'] ) {
                    echo '<li>';
                    echo '<a href="' . esc_url( $language['url'] ) . '">' . esc_html( $language['native_name'] ) . '</a>';
                    echo '</li>';
                }
            }

            echo '</ul>';
        }
    }
}

To display the WPML language switcher in your sidebar, you can add the following code to your theme’s sidebar template file (e.g., sidebar.php):

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
    <div id="secondary" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
        <?php wpsnippets_wpml_language_switcher(); ?>
    </div><!-- #secondary -->
<?php endif; ?>

Make sure to replace 'sidebar-1' with the appropriate sidebar ID or name in your theme.

The wpsnippets_wpml_language_switcher() function checks if the WPML plugin is active and retrieves the list of available languages using the icl_get_languages() function. It then loops through the languages and outputs a list item (<li>) for each language, excluding the currently active language. The language name and URL are properly escaped using esc_html() and esc_url() functions to ensure security.

The resulting language switcher is wrapped in a <ul> element with the class wpml-language-switcher, which you can style using CSS to match your theme’s design.

Examples

Example 1: Adding WPML language switcher in sidebar using a widget

This example demonstrates how to add the WPML language switcher in the sidebar of your WordPress site using a widget.

function wpsnippets_wpml_language_switcher_widget() {
    register_widget( 'WPML_Language_Switcher_Widget' );
}
add_action( 'widgets_init', 'wpsnippets_wpml_language_switcher_widget' );

Explanation:

  • The wpsnippets_wpml_language_switcher_widget function registers the WPML Language Switcher widget using the register_widget function.
  • The add_action function hooks the wpsnippets_wpml_language_switcher_widget function to the widgets_init action, ensuring that the widget is registered during the initialization of widgets.

Example 2: Adding WPML language switcher in sidebar using a shortcode

This example demonstrates how to add the WPML language switcher in the sidebar of your WordPress site using a shortcode.

function wpsnippets_wpml_language_switcher_shortcode() {
    return do_shortcode( '[wpml_language_switcher]' );
}
add_shortcode( 'wpml_language_switcher_sidebar', 'wpsnippets_wpml_language_switcher_shortcode' );

Explanation:

  • The wpsnippets_wpml_language_switcher_shortcode function returns the WPML language switcher shortcode [wpml_language_switcher].
  • The add_shortcode function registers the wpml_language_switcher_sidebar shortcode and associates it with the wpsnippets_wpml_language_switcher_shortcode function.

Example 3: Adding WPML language switcher in sidebar using a custom template tag

This example demonstrates how to add the WPML language switcher in the sidebar of your WordPress site using a custom template tag.

function wpsnippets_wpml_language_switcher_sidebar() {
    if ( function_exists( 'wpml_language_switcher' ) ) {
        wpml_language_switcher( array( 'type' => 'dropdown' ) );
    }
}

Explanation:

  • The wpsnippets_wpml_language_switcher_sidebar function checks if the wpml_language_switcher function exists (provided by WPML plugin).
  • If the function exists, it is called with an array parameter specifying the language switcher type as a dropdown.
Last updated on October 18, 2023. Originally posted on November 18, 2023.

Leave a Reply