Display Current Language in WordPress Site Header Easily
How to show current language in wordpress header;
Display current language in wordpress site header;
Wordpress header current language display;
Add current language to wordpress header;
Show current language on wordpress page header;
Wordpress display current language in header;
Current language in wordpress header code;
Wordpress header language indicator;
How to add language switcher to wordpress header;
Wordpress 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
<?php
// Function to display the current language in the WordPress header
function wp_dudecom_display_current_language() {
// Check if the Polylang plugin is active
if (function_exists('pll_current_language')) {
// Get the current language name
$current_language = pll_current_language('name');
// Escape the output for security
echo esc_html($current_language);
} elseif (function_exists('icl_get_languages')) {
// Check if WPML is active and get the current language
$languages = icl_get_languages('skip_missing=0');
if (!empty($languages)) {
foreach ($languages as $language) {
if ($language['active']) {
// Escape the output for security
echo esc_html($language['native_name']);
break;
}
}
}
} else {
// Default message if no multilingual plugin is active
echo esc_html__('Language not detected', 'text-domain');
}
}
// Hook the function to display the current language in the header
add_action('wp_head', 'wp_dudecom_add_language_to_header');
function wp_dudecom_add_language_to_header() {
?>
<style>
.wp-dudecom-language-indicator {
position: absolute;
top: 10px;
right: 10px;
background-color: #f1f1f1;
padding: 5px 10px;
border-radius: 5px;
font-size: 14px;
}
</style>
<div class="wp-dudecom-language-indicator">
<?php wp_dudecom_display_current_language(); ?>
</div>
<?php
}
?>
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:
- Open your WordPress admin dashboard.
- 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. - Locate and open the
functions.php
file of your active theme or your custom plugin file. - Copy and paste the provided code snippet into the file.
- Save the changes.
- 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.