Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem: I'd like to add different code depending on the languages

Solution: the code on the link below could be adapted for any type of code you want to change according to the languages.

Relevant Documentation: https://wpml.org/forums/topic/multilanguage-facebook-pixel/#post-7273051

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

Last updated by ugurcank 4 years, 5 months ago.

Assisted by: Alejandro.

Author Posts
October 19, 2020 at 6:12 pm #7260617

ugurcank

Tell us what you are trying to do?
I had support but I didn't respond for too long. I need to add code to make the english pages load in the english pixel and the same with dutch pages and pixel.

Is there any documentation that you are following?
https://wpml.org/forums/topic/facebook-pixel-wpml/

What is the link to your site?
hidden link

October 21, 2020 at 9:08 am #7273051

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hello!

Well, this would be out of the scope of this support since it falls under the category of custom coding, however, i want to point you in the right direction (i just can't debug the code).

A function like this one added to your active theme's function.php file will probably do the trick:

function pixelByLanguages(){
	$current_language= apply_filters( 'wpml_current_language', NULL);

	/* Note that XX refers to the language code. i.e: en - English, es-Spanish, it-italian, etc. */
	if($current_language == 'XX'){ 
		echo "PIXEL JS CODE GOES HERE INSIDE <script></script> TAG";

	} else{
	 /*this one will be used as DEFAULT in case no language defined above is found. you can remove this part if you don't want to show the pixel in one of the languages or if you want to instead show it in all the other languages except the one defined above */

		echo "PIXEL JS CODE GOES HERE INSIDE <script></script> TAG";
	}


}
add_action('wp_head','pixelByLanguages');
/* you can change wp_head to wp_footer if you want to add the code in the footer*/

Documentation and references:
- WP_HEAD ACTION: https://developer.wordpress.org/reference/hooks/wp_head/
- CURRENT LANGUAGE RETRIEVAL: https://wpml.org/wpml-hook/wpml_current_language/

Regards.

October 24, 2020 at 2:13 am #7297621

ugurcank

Hey thanks for the reply Alejandro.

I tried to put in the code with no success unfortunately. I understand this is not in the scope of the support but every tip is more then welcome. In the screenshot you can see how I put in the code and what happens when its loaded in. When checking the pixel with Facebook pixel helper it doesn't show up the english one.

3.png
2.png
1.png
October 26, 2020 at 8:53 am #7305687

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hi there,

Indeed what you did wouldn't work because of a very simple yet tricky to see issue:

you are echoing the code using double quotes, right? however the pixel is also using double quotes for a few things so you have 2 ways to fix this:

1) Make sure all double quotes (") in the facebook pixel are replaced by single quotes ('). This won't mess up with the pixel script, so don't worry about that.

2) Add a backslash right before the double quotes inside the facebook pixel script (it would look like this --> \")

That should fix the problem.

Regards.