[+] Add different code per language (Google Analytics, tags, Facebook and etc)

Often clients need to define different code/scripts for different languages in the header of their website. For that, we can use WP hook and WPML language code to simply define it.

Add next code to functions.php of your heme and adjust the code and cases:

add_action( 'wp_head', function(){
?>

<?php if(ICL_LANGUAGE_CODE=='fr'){?>

<!-- Content for FR language here -->

<?php } elseif(ICL_LANGUAGE_CODE=='en'){?>

<!-- Content for EN language here -->

<?php } ?>

<?php
});

  • You can use elseif further cases.
  • You can use a different hook if you need to set it somewhere differently, for example, body or footer.
  • https://www.w3schools.com/php/php_if_else.asp
  • https://developer.wordpress.org/reference/hooks/wp_head/

Support Thread Example

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments