Add a Language Switcher Button to WordPress Menu Easily

How to add language switcher to wordpress menu; Add language switcher button in wordpress; Wordpress language switcher menu setup; Steps to add language switcher in wordpress; Wordpress add language switcher to navigation; Integrate language switcher in wordpress menu; Wordpress menu language switcher guide; How to display language switcher in wordpress; Wordpress language switcher menu tutorial; Adding language switcher to wordpress site;

Explanation

To add a language switcher to your WordPress menu, you can use either the WPML or Polylang plugins. This code snippet helps you integrate a language switcher into your site's primary navigation menu.

  • WPML: If WPML is active, the code checks for available languages. It adds each language as a menu item, except for the currently active one. This way, users can switch to any other available language.
  • Polylang: Similarly, if Polylang is active, the code lists all available languages in the menu, excluding the current language. This allows users to easily switch languages from the menu.

The code uses WordPress filters to modify the menu items. It checks if the plugins are active and then appends the language options to the menu. Make sure your menu location is set to 'primary' for this to work.

Code

<?php
// Add a language switcher to the WordPress menu using WPML or Polylang

// Check if WPML is active and add language switcher to menu
function wp_dudecom_add_language_switcher_to_menu($items, $args) {
    if (function_exists('icl_get_languages') && 'primary' === $args->theme_location) {
        $languages = icl_get_languages('skip_missing=0');
        if (!empty($languages)) {
            foreach ($languages as $l) {
                if (!$l['active']) {
                    $items .= '<li class="menu-item"><a href="' . esc_url($l['url']) . '">' . esc_html($l['native_name']) . '</a></li>';
                }
            }
        }
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wp_dudecom_add_language_switcher_to_menu', 10, 2);

// Check if Polylang is active and add language switcher to menu
function wp_dudecom_add_polylang_language_switcher($items, $args) {
    if (function_exists('pll_the_languages') && 'primary' === $args->theme_location) {
        $languages = pll_the_languages(array('raw' => 1));
        if (!empty($languages)) {
            foreach ($languages as $l) {
                if (!$l['current_lang']) {
                    $items .= '<li class="menu-item"><a href="' . esc_url($l['url']) . '">' . esc_html($l['name']) . '</a></li>';
                }
            }
        }
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wp_dudecom_add_polylang_language_switcher', 10, 2);
?>

Instructions

To add a language switcher button to your WordPress menu using WPML or Polylang, follow these steps:

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have either the WPML or Polylang plugin installed and activated.
  • Set your menu location to 'primary' in your WordPress theme settings.

Implementation Steps:

  1. Open your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor or use an FTP client to access your theme files.
  3. Locate and open the functions.php file of your active theme.
  4. Copy and paste the provided code snippet into the functions.php file.
  5. Save the changes to the functions.php file.
  6. Verify that the language switcher appears in your site's primary navigation menu.

If you encounter any issues or need further assistance, consider reaching out to wp-dude.com for expert help with implementation or more advanced functionality.