How to determine the current language of a wordpress page when using polylang?

wordpress get current language polylang
pll_save_post_translations
polylang get posts by language
wpml get current language code
pll_default_language
pll_home_url
pll languages list
pll_translate_string

I search for a variable that outputs the currently used language of the polylang plugin. Something like:

if($curlang == "en") {
  ...
}

The solution was simply:

if (get_locale() == 'en_GB') {

How to know the current language in the theme, IMPORTANT: when using one or more of these function, you *must* check for the your site will badly break with a fatal error at next Polylang update (as WordPress 'raw' => use this to create your own custom language switcher (​default:0). Go in Languages > Languages. We are going to walk through all the settings available to define the languages. 1. Add a language. Choose a language in the dropdown list. Polylang comes with a list of more than 90 predefined languages. If your language is not available in the predefined list, you can create it yourself by filling the relevant fields.

Function reference - Polylang, Hi, In a function in functions.php i need to detect the language of the page , is there Viewing 2 replies - 1 through 2 (of 2 total) For me it's not 100% clear if you want to get the current language (set by user or automatically set by polylang for  returns either the full name, or the WordPress locale (just as the WordPress core function ‘get_locale’ or the slug ( 2-letters code) of the current language. pll_get_post Returns the post (or page) translation

To show current language, you can use:

 <?php echo $lang=get_bloginfo("language"); ?>

Plain and simple

Detect language page in code, The main language of a post should be saved in a post meta field. There is no way to detect that automatically. Even Google's heuristics fail  Polylang enables you to add languages to your WordPress site and also manage the translation process. It supports both manual and automatic translations, and can even integrate a translation management system in case you want to bring in some professionals. Key features: Supports manual and automatic translations. Add an unlimited number of languages.

This plugin is documented rather good in https://polylang.wordpress.com/documentation.

Switching post language

The developers documentation states the following logic as a means to generate URL's for different translations of the same post

<?php while ( have_posts() ) : the_post(); ?>
<ul class='translations'><?php pll_the_languages(array('post_id' =>; $post->ID)); ?></ul>
<?php the_content(); ?>
<?php endwhile; ?>

If you want more influence on what is rendered, inspet pll_the_languages function and copy it's behaviour to your own output implementation

Switching site language

As you want buttons to switch language, this page: https://polylang.wordpress.com/documentation/frequently-asked-questions/the-language-switcher/ will give you the required info.

An implementation example:

<ul><?php pll_the_languages();?></ul>

Then style with CSS to create buttons, flags or whatever you want. It is also possible to use a widget for this, provided by te plugin

Getting current language

All plugins functions are explained here: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/

In this case use:

pll_current_language();

detect the language a post is written in, Know which translations will need the Right-to-Left (RTL) languages, and ensure How to Translate a WordPress Website With Polylang (In 4 Steps) click the provided link for setting all site content to the default language. The function will return an array of arrays, one array per language with the following entries: [id] => language id. [slug] => language code used in urls. [name] => language name. [url] => url of the translation. [flag] => url of the flag. [current_lang] => true if this is the current language, false otherwise.

Simple:

if(pll_current_language() == 'en'){
   //do your work here
}

The Beginner's Guide to Translating a WordPress Website With , Jérémy Heleine introduces Polylang, a free plugin that you can use to turn your WordPress website into a fully featured multi-language platform. By default, this group is named “polylang” but you can define your own group,  I setup the language switcher but it doesn’t appear on frontend. To avoid 404 errors, Polylang does not display a language if there is no published content (post or page) in that language. If there is no content in any language, then the language switcher does not appear at all. Is it possible to display a language switcher without using the

Multilingual WordPress Websites with Polylang, How to create multilingual WordPress site with Polylang. Details: By Find out how to set up a language selector. Set your default language using star icon:. Polylang Shortcode. GitHub Gist: instantly share code, notes, and snippets.

Multilingual WordPress Site with Polylang, If you are writing a wordpress multi language site like me, you can end up with Polylang, one of the best free wordpress translation plugin. Step 2: Configure Polylang for Your Required Languages. Now you’re ready to configure Polylang so it supports your desired languages. Knowing your language codes will help you ensure you’re selecting the right options in Polylang. To get started, visit the Languages screen on your WordPress dashboard. From here, you’ll find a drop-down

Detect current language of a post with PHP & JS (using Polylang , Create a Multilingual WordPress site with Polylang tutorial By default, the plugin will detect Duration: 14:28 Posted: Feb 23, 2017 Setup Polylang at first, add at least TWO languages, one of them make your default language (in Polylang) Add content to your default Polylang language – if Polylang asks you to apply existing content to it, you should most likely click “ok” and proceed – it works really fine 🙂

Comments
  • fixed grammar; paragraphs
  • @AlexandreBourlier Are U using the code like that? if (get_locale() == 'en_GB') { //do something }
  • Worked like a pice of cake
  • Thanks for you reply. I think it is not exactly what I searched for. I have a Button on the Header of every page. I want to have two links, one for the german and another for the english version of the site.
  • Updated the answer. Please refer to the documentation provided by the developer as well. If this answer combined with the documentation is not enough, please refine your question further
  • I already translated the button caption but it is about the href link behind it.
  • In fact I only need a variable in php that tells me what language is actually shown and then I can output one of the two links with a if statement.
  • Sorry but i think i have to withdraw my answer. I'm now completely confused about what the question is. The links generated by the above code will switch the language of the site. My previous answer switches the language of the post.