Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPML - Get current language within a another plugin

Tags:

wordpress

wpml

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.

like image 489
dkruchok Avatar asked Jul 15 '26 08:07

dkruchok


2 Answers

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

like image 189
Amor.o Avatar answered Jul 21 '26 04:07

Amor.o


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;
}
like image 24
Komal Gondhali Avatar answered Jul 21 '26 04:07

Komal Gondhali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!