Skip Navigation

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 3 replies, has 2 voices.

Last updated by Dražen Duvnjak 2 years, 9 months ago.

Assisted by: Dražen Duvnjak.

Author Posts
March 7, 2022 at 7:04 pm #10733287

Enrique Viegas

In a single WP installation I have WPML with different domain.

For each domain I have a different Google Tag Manager Code. GTM gives you to codes to install, one in the header and one after the body.

I have the to codes inserted via functions.php

How do I make this code change depending on different languages? My code gives me error in the site:

<?php

function my_custom_function() {
if(ICL_LANGUAGE_CODE=='fr'){

/// Add code in header
add_action('wp_head', 'add_code_header');
function add_code_header(){
?>

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'hidden link);
})(window,document,'script','dataLayer','GTM-NBC5W8C');</script>
<!-- End Google Tag Manager -->

<?php
};

/// Add code after body
add_action('wp_body_open', 'add_code_body');
function add_code_body(){
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="hidden link"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

<?php
};

} elseif(ICL_LANGUAGE_CODE=='de'){
<!---your code to german--->

} elseif(ICL_LANGUAGE_CODE=='fr'){
<!---your code to french---
Global site tag (gtag.js) - Google Analytics -->

} elseif(ICL_LANGUAGE_CODE=='es'){
<!---your code to spanish--->
}
}

?>

March 9, 2022 at 9:20 am #10747145

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello!

Welcome to WPML Support. I'll do my best to help you solve this issue.

You should first add your action and then you can use the ICL_LANGUAGE_CODE constant with a IF/Else statement as in example to execute different codes depending on language:

Example code, you will need to adapt for your needs:

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
});

More info:
- https://wpml.org/forums/topic/install-google-tag-manager-in-a-multiple-domain-website/
- https://wpml.org/forums/topic/using-google-tag-manager-on-multiple-domains/
- https://wpml.org/forums/topic/split-ga-code-for-each-language/#post-6429349

I hope I was helpful.

Regards,
Drazen

March 9, 2022 at 11:48 am #10748881

Enrique Viegas

Thank you so much! This worked when I added 2 languages, but If I have more, I tried to add pt-br and wordpress is giving me error, sorry Im not coder, its probably something silly to fix, this is that I want:

<?php



/////////// Add codes after <header> with conditional WPML

add_action( 'wp_head', function(){ ?>
 
<?php if(ICL_LANGUAGE_CODE=='ar'){?>
 <!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'<em><u>hidden link</u></em>);
})(window,document,'script','dataLayer','GTM-58BVJ2N');</script>
<!-- End Google Tag Manager -->

<?php if(ICL_LANGUAGE_CODE=='pt-br'){?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'<em><u>hidden link</u></em>);
})(window,document,'script','dataLayer','GTM-PDMM8KF');</script>
<!-- End Google Tag Manager -->

 
<?php } elseif(ICL_LANGUAGE_CODE=='en'){?>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'<em><u>hidden link</u></em>);
})(window,document,'script','dataLayer','GTM-NBC5W8C');</script>
<!-- End Google Tag Manager --> 
<?php } ?>


 
<?php
});      





/////////// Add codes after <body> with conditional WPML
add_action( 'wp_body_open', function(){ ?>
 
<?php if(ICL_LANGUAGE_CODE=='ar'){?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="<em><u>hidden link</u></em>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

<?php if(ICL_LANGUAGE_CODE=='pt-br'){?>
 <!-- Google Tag Manager (noscript) -->
<noscript><iframe src="<em><u>hidden link</u></em>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
 
<?php } elseif(ICL_LANGUAGE_CODE=='en'){?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="<em><u>hidden link</u></em>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) --> 
<?php } ?>
 
<?php
});    
March 9, 2022 at 11:57 am #10748967

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for getting back, glad to hear that was helpful.

You should use

elseif

for other cases as it is for EN, if is only for the first rule.

Please check the tickets I shared in the previous reply and the next link for more context:

- hidden link

Let me know how it goes and if you still need our help.

Kind regards,
Drazen