Last updated on October 18, 2023

WPML language switcher shortcode

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

Add WPML language switcher using shortcodes.

The WPML language switcher shortcode allows you to display a language switcher on your WordPress site, making it easy for users to switch between different languages. This can be useful if you have a multilingual website and want to provide a seamless experience for your visitors.

To use the WPML language switcher shortcode, you can simply add the following code to your WordPress theme files or a custom shortcode:

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

Once you have added this code, you can use the [wpml_language_switcher] shortcode anywhere in your content to display the language switcher.

For example, if you want to display the language switcher in a sidebar widget, you can add a Text widget to your sidebar and enter [wpml_language_switcher] as the widget content.

This code snippet creates a custom shortcode wpml_language_switcher that calls the wpsnippets_wpml_language_switcher_shortcode function. Inside the function, we use the do_shortcode function to generate the language switcher HTML using the [wpml_language_selector_widget] shortcode provided by WPML. Finally, the shortcode is registered using the add_shortcode function.

By using this code snippet, you can easily add a language switcher to your WordPress site and enhance the user experience for multilingual visitors.

Examples

Example 1: Displaying the WPML language switcher shortcode in a widget

This use case demonstrates how to display the WPML language switcher shortcode in a widget area on your WordPress site.

function wpsnippets_display_language_switcher_widget() {
    echo do_shortcode('[wpml_language_selector_widget]');
}
add_action('widgets_init', 'wpsnippets_register_language_switcher_widget');

function wpsnippets_register_language_switcher_widget() {
    register_widget('WPSnippets_Language_Switcher_Widget');
}

class WPSnippets_Language_Switcher_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'wpsnippets_language_switcher_widget',
            'Language Switcher',
            array('description' => 'Displays the WPML language switcher shortcode.')
        );
    }

    public function widget($args, $instance) {
        echo $args['before_widget'];
        echo $args['before_title'] . $args['widget_name'] . $args['after_title'];
        wpsnippets_display_language_switcher_widget();
        echo $args['after_widget'];
    }
}

The code registers a custom widget called “Language Switcher” that displays the WPML language switcher shortcode. The wpsnippets_display_language_switcher_widget() function is used to output the shortcode, and it is hooked into the widgets_init action to register the widget. The widget class extends the WP_Widget class and defines the widget’s name, description, and the widget() method, which outputs the widget’s content.

Example 2: Displaying the WPML language switcher shortcode in a menu

This use case demonstrates how to display the WPML language switcher shortcode in a WordPress menu.

function wpsnippets_display_language_switcher_menu_item($items, $args) {
    if ($args->theme_location == 'primary') {
        $items .= '<li>' . do_shortcode('[wpml_language_selector_widget]') . '</li>';
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wpsnippets_display_language_switcher_menu_item', 10, 2);

The code adds a filter to the wp_nav_menu_items hook, which allows us to modify the menu items before they are outputted. In this example, we check if the menu being displayed is the “primary” menu, and if so, we append a new menu item to the end of the menu using the WPML language switcher shortcode.

Example 3: Displaying the WPML language switcher shortcode in a template file

This use case demonstrates how to display the WPML language switcher shortcode directly in a template file.

echo do_shortcode('[wpml_language_selector_widget]');

The code uses the do_shortcode() function to directly output the WPML language switcher shortcode in a template file. This allows you to place the language switcher anywhere within your theme’s template files, giving you full control over its placement and styling.

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

Leave a Reply

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