• Hi,
    today I needed to get the current language w/o the flag.
    But the function pll_current_language() returns only the localization.

    Because of this I added a new pll-function which returns the language name w/o the flag.
    Possible arguments are: echo, show_flag, show_name, link, display_name_as
    All arguments except link work as same as in pll_the_languages.
    The argument link returns the current language as link or not.

    You can now call <?php echo pll_current_language2(array('show_flag' => 1)); ?> to get the current language name and the corresponding flag

    Add this to the include/core.php:

    // displays (or returns) the language switcher
    function current_language2($args = '') {
    	$defaults = array(
    		'echo' => 0, // don't echoes the output
    		'link' => 0, // don't get as link
    		'show_flag' => 0, // don't show flags
    		'show_name' => 1, // show language names
    		'display_name_as' => 'name', // valid options are slug and name
    	);
    	extract(wp_parse_args($args, $defaults));
    
    	$output = '';
    
    	foreach ($this->get_languages_list(false) as $language) {
    		// continue if not current language
    		if ($this->curlang->term_id != $language->term_id) {
    			continue;
    		}
    
    		$url = $force_home ? null : $this->get_translation_url($language);
    		$url = apply_filters('pll_the_language_link', $url, $language->slug, $language->description);
    
    		$url = isset($url) ? $url : $this->get_home_url($language); // if the page is not translated, link to the home page
    
    		$flag = $show_flag ? $this->get_flag($language) : '';
    		$name = $show_name || !$show_flag ? esc_html($display_name_as == 'slug' ? $language->slug : $language->name) : '';
    
    		if($link == 1) {
    			$output .= sprintf("<a hreflang='%s' href='%s'>%s</a>",
    				esc_attr($language->slug), esc_url($url), $show_flag && $show_name ? $flag.'&nbsp;'.$name : $flag.$name);
    		}
    		else {
    			$output .= sprintf("%s",
    				$show_flag && $show_name ? $flag.'&nbsp;'.$name : $flag.$name);
    		}
    	}
    
    	if(!$echo)
    		return $output;
    	echo $output;
    }

    Add this to the include/api.php:

    // returns the current language
    function pll_current_language2($args = '') {
    	global $polylang;
    	return isset($polylang) ? $polylang->current_language2($args) : false;
    }

    Maybe this will help someone 😉

    http://wordpress.org/extend/plugins/polylang/

The topic ‘[Plugin: Polylang] get current language’ is closed to new replies.