agiannis
1/24/2020 - 10:55 AM

Shortcode for showing content when using multi-language wordpress with polylang plugin

labels: php, wordpress, polylang, shortcode , multi-language

if (function_exists('pll_current_language')) {
  add_shortcode('polylang', array('shortcode_polylang'));
}

/**
 * Shortcode for polylang
 * Usage: [polylang lang="en"]English[/polylang][polylang lang="sp"]Spanish[/polylang]
 * @param array $attrs
 * @param string $content
 * @return string
 */
function shortcode_polylang($attrs, $content = '')
{
    if (empty($content)) return '';
    extract(shortcode_atts(['lang' => ''], $attrs));
    if (empty($lang)) return "<h3>You must specify 'lang' using shortcode: polylang</h3>";
    return ($lang == pll_current_language()) ? $content : '';
}
add_shortcode('polylang', array('shortcode_polylang'));