Snippet

Display Current Language in WordPress Site Header Easily

How to show current language in wordpress headerDisplay current language in wordpress site headerWordpress header current language displayAdd current language to wordpress headerShow current language on wordpress page headerWordpress display current language in headerCurrent language in wordpress header codeWordpress header language indicatorHow to add language switcher to wordpress headerWordpress header show current language

Explanation

Want to show the current language on your WordPress site header? This code snippet does just that, using popular multilingual plugins like Polylang or WPML.

Here's how it works:

  • If you're using Polylang, the code checks if it's active and fetches the current language name.
  • If you're using WPML, it checks for active languages and displays the current one.
  • If neither plugin is active, it simply shows a message saying "Language not detected."

The language name is displayed in a neat little box at the top right of your site, styled with a bit of CSS for a clean look.

Just make sure you have either Polylang or WPML installed and activated for this to work. If not, you'll see the default message.

Code

1<?php 2// Function to display the current language in the WordPress header 3function wp_dudecom_display_current_language() { 4 // Check if the Polylang plugin is active 5 if (function_exists('pll_current_language')) { 6 // Get the current language name 7 $current_language = pll_current_language('name'); 8 9 // Escape the output for security 10 echo esc_html($current_language); 11 } elseif (function_exists('icl_get_languages')) { 12 // Check if WPML is active and get the current language 13 $languages = icl_get_languages('skip_missing=0'); 14 if (!empty($languages)) { 15 foreach ($languages as $language) { 16 if ($language['active']) { 17 // Escape the output for security 18 echo esc_html($language['native_name']); 19 break; 20 } 21 } 22 } 23 } else { 24 // Default message if no multilingual plugin is active 25 echo esc_html__('Language not detected', 'text-domain'); 26 } 27} 28 29// Hook the function to display the current language in the header 30add_action('wp_head', 'wp_dudecom_add_language_to_header'); 31 32function wp_dudecom_add_language_to_header() { 33 ?> 34 <style> 35 .wp-dudecom-language-indicator { 36 position: absolute; 37 top: 10px; 38 right: 10px; 39 background-color: #f1f1f1; 40 padding: 5px 10px; 41 border-radius: 5px; 42 font-size: 14px; 43 } 44 </style> 45 <div class="wp-dudecom-language-indicator"> 46 <?php wp_dudecom_display_current_language(); ?> 47 </div> 48 <?php 49} 50?>

Instructions

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

Prerequisites:

  • Ensure you have either the Polylang or WPML plugin installed and activated.

Implementation Steps:

  1. Open your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php. Alternatively, use a code editor if you are working with a custom plugin file.
  3. Locate and open the functions.php file of your active theme or your custom plugin file.
  4. Copy and paste the provided code snippet into the file.
  5. Save the changes.
  6. Visit your website to ensure the current language is displayed in the header as expected.

If you encounter any issues or need further customization, consider reaching out to wp-dude.com for expert assistance.