Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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' );
@colinschwebke

This comment has been minimized.

Copy link

colinschwebke commented Jul 13, 2016

Hello,

i want to use two shortcodes for polylang

your one with [polylang]

and this one with:

// [polylang lang="en"]English[/polylang][polylang lang="de"]Deutsch[/polylang]
function polylang_shortcode($atts, $content = null)
{
if (empty($content))
return '';
extract( shortcode_atts( array('lang' => ''), $atts ) );
if (empty($lang))
return "

You must specify 'lang' using shortcode: polylang

";

return ($lang == pll_current_language()) ? $content : '';
}
add_shortcode('polylang', 'polylang_shortcode');

But i am not able to combine them on my own. Could anyone help?
polylang

Regards
Colin

@colinschwebke

This comment has been minimized.

Copy link

colinschwebke commented Jul 13, 2016

Hello,

meanwhile i used my brain and found the solution. both functions defined the same shortcode.

thatswhy i change the code that way:

// Polylang Shortcode - https://wordpress.org/plugins/polylang/
// Add this code in your functions.php
// Put shortcode [polylanguage] to post/page for display flags

function polylanguage_shortcode() {
ob_start();
pll_the_languages(array('show_flags'=>1,'show_names'=>0));
$flags = ob_get_clean();
return $flags;
}
add_shortcode( 'polylanguage', 'polylanguage_shortcode' );

Now the function is working. Unfortunately the flags are ordered under each other and having a dot. I want them beneath each other and without a dot. In addition i want the flags a bit bigger.
Could anyone help?

Regards
Colin

@picassy

This comment has been minimized.

Copy link

picassy commented Oct 25, 2016

this works for me
<?php echo polylang_shortcode(); ?>

@Aldo-f

This comment has been minimized.

Copy link

Aldo-f commented Feb 7, 2018

Does this works?
[polylang lang="en"]English[/polylang][polylang lang="de"]Deutsch[/polylang]

@andrewvink

This comment has been minimized.

Copy link

andrewvink commented Mar 15, 2018

I adjusted the shortcode a bit to have more control over the styling

// Add this to your functions.php file

function polylang_flags_shortcode() {
    ob_start();
    pll_the_languages(array('show_flags'=>1,'show_names'=>0));
    $flags = ob_get_clean();
    return '<ul class="polylang-flags">' . $flags . '</ul>';
}
add_shortcode('POLYLANG', 'polylang_flags_shortcode');

Then add this CSS to your theme, either via the customize panel "Custom CSS" or a wp_enqueue_style

/* Polylang Flags Inline */

.polylang-flags {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.polylang-flags li {
    display: inline;  
}

Use the Shortcode somewhere on your site

[POLYLANG]
@dinamicore

This comment has been minimized.

Copy link

dinamicore commented Jun 19, 2018

Hello; I used the PHP code to add a shortcode, and the css to style. Both working fine.

Question: how can I hide the current language? What code do I need to add or modify?

Thanks

@strarsis

This comment has been minimized.

Copy link

strarsis commented Jun 19, 2018

@taninbkk: It would be nice if this could be put into a WordPress plugin.

@kiditran

This comment has been minimized.

Copy link

kiditran commented Sep 12, 2018

@andrewvink : thank you so much for this sharing. I just add your code to my theme, it actually works fine.

@mstudioIL

This comment has been minimized.

Copy link

mstudioIL commented Dec 6, 2018

How do I show only languages names?

@Ruggierini

This comment has been minimized.

Copy link

Ruggierini commented May 12, 2019

I adjusted the shortcode a bit to have more control over the styling

// Add this to your functions.php file

function polylang_flags_shortcode() {
    ob_start();
    pll_the_languages(array('show_flags'=>1,'show_names'=>0));
    $flags = ob_get_clean();
    return '<ul class="polylang-flags">' . $flags . '</ul>';
}
add_shortcode('POLYLANG', 'polylang_flags_shortcode');

Then add this CSS to your theme, either via the customize panel "Custom CSS" or a wp_enqueue_style

/* Polylang Flags Inline */

.polylang-flags {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.polylang-flags li {
    display: inline;  
}

Use the Shortcode somewhere on your site

[POLYLANG]

Hello,

hope its not to late haha, I managed to do it and show the language instead of flags, and it shows and arrow besides the names of the languages but i cant find a way to change the color of that arrow.

Anyone has an idea of how to change it? if someone if still on this haha.
Thanks in advance

@xalunda

This comment has been minimized.

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

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.