[AUTOMATIQUE] Cet article a plus de 5 ans.
Il se peut donc que les informations qu'il fournit ne soient plus totalement exactes.

If like me you do not have a site fully translated, but only a few posts, you certainly want to inform users that a particular post is available in another language.

I addressed the issue by writing the docs of my plugins. I starded writting them in English to make them accessible to the greatest number, I then thought it was a shame not to translate them into French! So I translated those articles, but I have not found a way to simply switch from French to English and vice versa. So I read Polylang documentation – the plugin that allowed me to make my multilingual site – and I found the elements I needed.

Here’s the function that you can use to detect if a translation exists for an post:

function bweb_translation_available($content){
	if ( !is_singular( 'post' ) )
	return $content;
	global $polylang;
	$translationIds = $polylang->model->get_translations('post', get_the_ID());
	$currentLang = pll_get_post(get_the_ID(), pll_current_language());
	foreach ($translationIds as $key=>$translationID){
		if($translationID != $currentLang){
			$availableLang = $polylang->model->get_languages_list();
			foreach( $availableLang as $lang){
				if($key == $lang->slug){
					$newContent.= '<a class="post-transltation" href="' . get_permalink($translationID) . '">';
						if ( $lang->slug == 'fr'){
							$newContent.= 'Cette page est également disponible en ' . $lang->name . ' <img src="' . $lang->flag_url . '" alt="' . $lang->name . '">';
						} else {
							$newContent.= 'This page is also available in ' . $lang->name . ' <img src="' . $lang->flag_url . '" alt="' . $lang->name . '">';
					$newContent.= '</a>';
	$newContent.= $content;
	return $newContent;
add_filter('the_content', 'bweb_translation_available',11); 


The function will automatically add a link to switch language at the top of the content if it is available in another language. In my case, I restricted the filter on the Article page type only.

If you’re not comfortable with CSS, here is the style that I use on bweb:

.post-transltation {
	display: inline-block;
	width: 100%;
	padding: 15px 25px;
	text-decoration: none;
	font-size: 1.05em;
	margin: 0 0 25px 0;
	color: #fff;
	background-color: #db3939;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
.post-transltation:active {
	color: #fff;
	background-color: #444;



Post written byBrice CAPOBIANCO

WordPress addict and self-taught. I love to learn and to create, then to share…
Founder of bweb.
Share this post

Your email address will not be published. Required fields are marked *

Show 16 comments


  1. Thank you! I am implementing this on my site right now, and it works like a charm right out of the box. Soon to be published Again. Thank you!

  2. good day – wow just great . one question. does this code run in the newest version of wordpress too!? look forward to hear from you greetings

    • To be honest, I don’t know if it still works, but it’s easy to check : Try the function!

  3. Hello, your assistance was of great help. I wanted to know if you could help to solve the following problem. When using the search function, my cms site only looks in the current language. I want it to search ALL LANGUAGES. Is this possible? Thank you in advance.

    • Hi,
      I think it’s possible, but I don’t know how.
      I suggest you to open a new topic on the related plugin support forum.

  4. Hello Brice, I’m impressed with the result. Thank you for the solution!
    However, this does not completely fit my requirements.
    Well, I have a language switcher at the very top right of my WP CMS site. So a visitor has the freedom to switch language on an article which does not have a translated version. In such cases, I want to display a “COMING SOON!!” message. Is there any function that could detect when a translation is not available and then display this message as the message body?

    • I could really use the same thing as you, Shaded. Any help with this would be immensely appreciated!! I spent a couple hours trying to figure it out, but I didn’t have any luck. Thanks!

  5. Hello, I am a total newbie to wordpress. Can you please specify where exactly do we add this, as in which file? And also the significance of the number 11 and the string ‘the_content’ in the add_filter method?

    • Hi Shahed,
      You have to paste the above function in your functions.php file (you’ll find it in your theme folder).
      The number 11 referred to the priority of the add_filter hook triggered on “the_content”. That mean the above function will be called after the 10th function triggered on this hook (if it exists).
      To finish, “the_content” is used to display posts content, this is the place where the content you filled in the WYSIWYG is displayed. This allows to modify the output of that content by appending or filtering.
      You’ll find more information about that here : codex.wordpress.org/Plugi…he_content

  6. Works like a charm, I´m relly newbie in wordpress, but it would be nice to set position at the end of content, could you help me to do so?

  7. Thank you for sharing the code! Works great! Can you help me with getting this to work on pages and custom posttypes too?