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_widgetfunction registers the WPML Language Switcher widget using theregister_widgetfunction. - The
add_actionfunction hooks thewpsnippets_wpml_language_switcher_widgetfunction to thewidgets_initaction, 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_shortcodefunction returns the WPML language switcher shortcode[wpml_language_switcher]. - The
add_shortcodefunction registers thewpml_language_switcher_sidebarshortcode and associates it with thewpsnippets_wpml_language_switcher_shortcodefunction.
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_sidebarfunction checks if thewpml_language_switcherfunction exists (provided by WPML plugin). - If the function exists, it is called with an array parameter specifying the language switcher type as a dropdown.
