This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 10 replies, has 8 voices.

Last updated by janB-19 6 years, 2 months ago.

Assigned support staff: Harshad.

Author Posts
August 4, 2013 at 8:17 am #151638

Hassan

I know I can use something like

if (ICL_LANGUAGE_CODE == 'en') {
    some content
}

...to display content conditionally for different languages. This is a PHP solution. Is there a shortcode to use for this functionality inside posts and pages? Maybe something like:

[wpml_language code='en']
some content
[/wpml_language]
August 4, 2013 at 10:26 am #151653

Harshad

Dear Hassan,

There is no shortcode that you can use inside within post/pages, This can be a good idea to insert shortocodes.

I will add this as a feature request to our todo list so it can be added in upcoming version.

August 4, 2013 at 1:16 pm #151680

Hassan

Thanks.

Meanwhile, is it possible to create a temp shortcode from the ICL_LANGUAGE_CODE constant? Can you please ask the devs whether it is currently possible to create a custom shortcode with PHP? If so, please advise how to accomplish this.

August 5, 2013 at 10:20 am #151790

Harshad

Dear Hassan,

If you need custom programming work for your site, we recommend contacting one of our certified partners.
http://wpml.org/documentation/support/wpml-contractors/

September 25, 2013 at 5:53 pm #268974

Jeanne

Maybe a little late but here's what I did.

on my themes functions.php I added.

// wpml shortcodes --------------------

add_shortcode( 'wpml_language', 'wpml_find_language');


/* ---------------------------------------------------------------------------

 * Shortcode [wpml_language language="en"] [/wpml_language]

 * --------------------------------------------------------------------------- */

function wpml_find_language( $attr, $content = null ){
	
	extract(shortcode_atts(array(

		'language' => '',

	), $attr));
	
	$current_language = ICL_LANGUAGE_CODE;
	
	if($current_language == $language){
		$output = do_shortcode($content);
	}else{
		$output = "";
	}
		
	return $output;
}

So now I just need to use that shortcode and specify the language and if it matches the current page language it will show the custom text or code.

September 26, 2013 at 12:31 pm #269281

Hassan

Never late, Jeanne!

Thanks a million, this was helpful indeed 😀

April 17, 2014 at 2:09 pm #382757

alpaslanD

Hi Jeanne, I added my theme functions.php and tried on my theme options but didn't work. It appears like

[wpml_language language="en"]english content[/wpml_language] [wpml_language language="tr"]turkce icerik[/wpml_language]. 

What's wrong?

May 16, 2014 at 4:09 am #401408

richardR-2

This thread inspired a free plugin as a solution. http://wordpress.org/plugins/wpml-short-code-translator/

December 2, 2015 at 4:07 am #759346

danickG-2

I just so needed to say THANKS! I did the custom function solution and it works like a charm as is. I even wrap other shortcodes with the language shortcode and it's a breeze! Thanks again!

January 4, 2019 at 3:57 pm #3057149

basv-26

Dear Hassan, I was wondering if this request has allready been implemented. I am looking for a same kind of solution.

Regards
Bas van Dijk

October 2, 2019 at 8:23 am #4677753

janB-19

Hi guys, I have just solved the same problem.

Installing only the plugin is not enough. You need also to paste the code to functions.php

So.

1. Install WPML shortcode translator: https://wordpress.org/plugins/wpml-short-code-translator/
2. Paste this code to your theme's functions.php

// wpml shortcodes --------------------
 
add_shortcode( 'wpml_language', 'wpml_find_language');
 
 
/* ---------------------------------------------------------------------------
 
 * Shortcode [wpml_language language="en"] [/wpml_language]
 
 * --------------------------------------------------------------------------- */
 
function wpml_find_language( $attr, $content = null ){
     
    extract(shortcode_atts(array(
 
        'language' => '',
 
    ), $attr));
     
    $current_language = ICL_LANGUAGE_CODE;
     
    if($current_language == $language){
        $output = do_shortcode($content);
    }else{
        $output = "";
    }
         
    return $output;
}

3. You can now use WPML shortcodes this way:

[wpml_language language="en"]english content[/wpml_language] [wpml_language language="sk"]slovak content[/wpml_language]

Note: You can also use another shortcodes in this language shortcode, but never forget to close wpml shortcode.