What's the best way to detect the active language in a Polylang-enabled WordPress site?

I’m working on a multilingual WordPress site using Polylang. I need to figure out which language is currently being displayed on a page. Is there a built-in variable or function I can use to check this?

I’m looking for something simple that I can use in conditional statements. Ideally, it would work like this:

if (get_current_language() === 'english') {
    // Do something for English content
} elseif (get_current_language() === 'spanish') {
    // Do something for Spanish content
}

Does anyone know if Polylang provides a way to do this? Or is there another method I should use to detect the active language? Any help would be great!

hey danwilson85, i’ve used polylang before and there’s a simple way to do this. try pll_current_language(). it gives you the language code, like ‘en’ or ‘es’. just remember to check if the function exists first. it’s pretty handy for what you’re trying to do!

As someone who’s worked extensively with multilingual WordPress sites, I can confirm that Polylang’s pll_current_language() function is indeed the way to go. However, I’ve found it helpful to create a wrapper function for added flexibility. Here’s what I typically use:

function get_active_language() {
return function_exists(‘pll_current_language’) ? pll_current_language() : get_locale();
}

This approach has two advantages: it falls back to WordPress’s default locale if Polylang isn’t active, and it allows you to easily switch to a different multilingual plugin in the future without rewriting your conditionals. You can then use it like this:

if (get_active_language() === ‘en’) {
// English-specific code
}

Hope this helps streamline your multilingual development process!

I’ve dealt with this exact issue in a recent project. Polylang actually provides a straightforward function for this purpose: pll_current_language(). It returns the current language code, which is perfect for what you’re trying to achieve. Here’s how you can use it:

if (function_exists(‘pll_current_language’)) {
$current_lang = pll_current_language();
if ($current_lang === ‘en’) {
// English content logic
} elseif ($current_lang === ‘es’) {
// Spanish content logic
}
}

Just make sure to wrap it in a function_exists() check to avoid errors if Polylang isn’t active for some reason. This method has worked reliably for me across multiple multilingual sites.