Skip to content

Instantly share code, notes, and snippets.

@taninbkk
Last active April 20, 2025 09:29
Show Gist options
  • Save taninbkk/f95281b44b3762435777 to your computer and use it in GitHub Desktop.
Save taninbkk/f95281b44b3762435777 to your computer and use it in GitHub Desktop.
Polylang Shortcode
// Polylang Shortcode - https://wordpress.org/plugins/polylang/
// Add this code in your functions.php
// Put shortcode [polylang] to post/page for display flags
function polylang_shortcode() {
ob_start();
pll_the_languages(array('show_flags'=>1,'show_names'=>0));
$flags = ob_get_clean();
return $flags;
}
add_shortcode( 'polylang', 'polylang_shortcode' );
@xalunda
Copy link

xalunda commented Feb 13, 2020

Thanks!

For those who wants to tweak the generated labels, take a look a the official documentation to see the available options.

@AIMAR-S
Copy link

AIMAR-S commented May 22, 2020

Hi,
I was searching for a short code to get flags, but in wordpress administration the Language Switcher is not available, so i used this :

$pll_languges = pll_languages_list(['fields' => null]);

With this you will have an array of all Polylang PLL_Language objects , so you can get flag using ->flag on each array item.

And if you want only an array of flags and nothing else you can use :

$flags = pll_languages_list(['fields' => 'flag']);

Have fun :D

@anngro
Copy link

anngro commented Mar 7, 2022

Hiding the current language flag is possible with 'hide_current' =>1 in the above array:
pll_the_languages(array('show_flags'=>1,'show_names'=>0,'hide_current'=>1'));

All possible elements of the language switcher array can be found in plugins/polylang/include/switcher.php right at the beginning of the file (Polylang v3.1.4).

@maitseasi
Copy link

maitseasi commented Sep 6, 2022

Thanks a bunch for this!
Any guess on how to style the dropdown arrow for this? Im trying to replace it with a custom svg, but I couldnt get it with the one i targeted the rest of the menu drop-downs.

.et-menu .menu-item-has-children>a:first-child:after{ content: url(/dropdownarrow.svg); }

I couldnt get the target with inspect and was unable to find it in the plugin files (although im sure it is in there somewhere).

@sarathsivadasan
Copy link

How to set user wise [POLYLANG] in wordpress?

@yasircs4
Copy link

To modify your polylang_shortcode so it:
1. Only shows languages other than the current one, and
2. Displays both flag and language name,

You can update the function like this:

function polylang_shortcode() {
	ob_start();
	pll_the_languages(array(
		'dropdown'    => 0,
		'show_flags'  => 1,
		'show_names'  => 1,
		'hide_current' => 1, // Hides current language
		'raw'         => 0   // Output as HTML
	));
	return ob_get_clean();
}
add_shortcode('polylang', 'polylang_shortcode');

✅ Explanation:
• 'hide_current' => 1 – hides the current language.
• 'show_flags' => 1 – shows the language flag.
• 'show_names' => 1 – shows the language name.
• 'raw' => 0 – outputs HTML instead of raw array.

💡Usage:

Add [polylang] in any post, page, or widget, and it will display only the other available languages with both flag and name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment