wpml get current language

Wpml get current language in wordpress

by Expert of Tech

When creating a custom WordPress theme or editing the existing one, you may need to get the value of the current language in to a website using any type of condition. You can easily get this by using the native WPML variable ICL_LANGUAGE_CODE. We are showing the latest function for the wpml get current language

Copy the below code and paste it into the template file you want to create the condition. You may remove the script tag if you don’t need javascript coding.


<?php
 //wpml get current language in template
$current_language_code = apply_filters( 'wpml_current_language', null );
if($current_language_code == 'en'): 
?>
<script>
//javacode for English script here
</script>
 
<?php
elseif($current_language_code == 'it'):
?>
<script>
// javascript code for Italian
</script>
 
<?php
elseif($current_language_code == 'ru'):
?>
 
<script>
// javascript code for Russian
</script>
<?php
endif;
?>

To avoid any type of future error I recommend applying the below code.

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

You can see more about wpml get current language at https://wpml.org/documentation/support/wpml-coding-api/ 

Feel free to contact us if you face any difficulties, we will help you and learn more about the best WordPress theme.