5

I am trying to get current language code via ICL_LANGUAGE_CODE in my plugin but I can't , it's not defined , the result of this

echo "Code: ".ICL_LANGUAGE_CODE;

is

Code: ICL_LANGUAGE_NAME

When I trying the following

global $sitepress;
$current_language = $sitepress->get_current_language();
echo "Code:". $current_language ;

I get the blank screen.

I don't know in which way I can get it.

In the theme functions.php everything works fine. Thanks.

dkruchok
  • 1,829
  • 3
  • 19
  • 25
  • https://wpml.org/forums/topic/icl_language_code-not-available/ – mujuonly Aug 16 '16 at 11:36
  • No, it's doesn't help, I have tried also activate/deactivate my plugin and WPML plugin, checked the active_plugins list, my plugin is first – dkruchok Aug 16 '16 at 11:59

4 Answers4

8

You can use WPML default filter to get the current language like this

 $my_current_lang = apply_filters( 'wpml_current_language', NULL );

the result will containe your current language code like : ar , en , fr ... etc

Amor.o
  • 491
  • 8
  • 21
2

This constant is deprecated. Please use this instead https://wpml.org/wpml-hook/wpml_current_language/

Full new WPML API is listed here in case if you have more questions https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Or you can always ask question at official wpml.org forum :)

kkarpieszuk
  • 516
  • 3
  • 9
2

You can use the following code to get the current language code. I tried this in one of my plugin and its working.

if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
  echo ICL_LANGUAGE_CODE;
}
0

/* Get WPML Language using code: */

            <?php 
            $languages = icl_get_languages('skip_missing=0&orderby=code&order=DIR'); 
            foreach ($languages as $lang) { 
        ?>
        <li style = "list-style:none;">
            <a href="<?php echo $lang['url']; ?>">
                <img style="float:left; margin: 3px 5px 0 0;" src="<?php echo $lang['country_flag_url']; ?>" height="12" width="18" />
                <?php echo $lang['native_name'];  ?>
            </a>
        </li>
        <?php 
            } 
        ?>