Skip Navigation

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

Problem:
The client is experiencing an issue where the Japanese contact form isn't appearing despite using PHP code in their functions.php file to switch the contact form based on the browser language. The code uses

ICL_LANGUAGE_CODE

to determine the language and display the appropriate contact form.
Solution:
We recommend checking if the custom fields for the Japanese form are correctly set and contain the appropriate data. To debug, add the following line to log the value of

ICL_LANGUAGE_CODE

and ensure it matches 'ja':

error_log(ICL_LANGUAGE_CODE);

Additionally, consider using WPML's built-in automatic browser language redirection functionality, which might simplify the implementation and avoid custom coding. More details can be found here: Automatic Redirect Based on Browser Language.
If these suggestions do not resolve the issue, and custom coding is necessary, we recommend hiring a WPML contractor from WPML Contractors for further assistance.

Please note that this solution might be irrelevant if it's outdated or not applicable to your case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the issue persists, please open a new support ticket at WPML Support Forum.

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.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Mihai Apetrei 9 months, 1 week ago.

Assisted by: Mihai Apetrei.

Author Posts
July 23, 2024 at 6:25 pm #15994019

amandaL-9

Background of the issue:
I have a little bit of PHP code in my functions.php file that I use to switch the contact form from English to Japanese or Chinese, based on the browser language. It seems to work fine for the English form, but the Japanese form isn't appearing. (The Chinese translation has not been added to this site yet, but will be.) The current code is:

/**
* Change contact form from English to Japanese or Chinese
*
* @return string
*/
function fs_market_contact_form() {
if (!function_exists('get_field')) {
return;
}

// Get the current language via WPML.
$current_language = apply_filters( 'wpml_default_language', NULL );

// Display contact form based on translation.
if(ICL_LANGUAGE_CODE=='ja') {
$contact_form = get_field( 'contact_form_japan', get_the_ID() );
return $contact_form;
} elseif(ICL_LANGUAGE_CODE=='zh-hans') {
$contact_form = get_field( 'contact_form_china', get_the_ID() );
return $contact_form;
} else {
$contact_form = get_field( 'contact_form_english', get_the_ID() );
return $contact_form;
}
}
add_shortcode( 'fs_market_contact_form', 'fs_market_contact_form' );

Symptoms:
The Japanese form isn't appearing.

Questions:
Why isn't the Japanese form appearing?
Is there an issue with the current PHP code for switching the contact form based on the browser language?

July 23, 2024 at 8:50 pm #15994795

Mihai Apetrei
WPML Supporter since 03/2018

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hi there.

Thanks for reaching out. It sounds like there's an issue with your PHP code for switching the contact form based on the browser language.

From your code, it seems like you’re checking the current language using `ICL_LANGUAGE_CODE`. However, you might want to make sure that the custom fields for the Japanese form are set correctly.

To debug, you can add a line to log the value of `ICL_LANGUAGE_CODE` and ensure it matches `'ja'`:

error_log(ICL_LANGUAGE_CODE);

Also, make sure the custom field 'contact_form_japan' has the correct data and is accessible.

What I mentioned above is related to custom coding, which falls outside the purpose of our Support Policy, so there's not much else I can share about this. However, I still wanted to provide some recommendations that might help you figure things out.

Also, I am not sure why you don't prefer to use the feature that's coming built in WPML by default - WPML has the automatic browser language redirection functionality present in the settings and you can enable it:
https://wpml.org/documentation/getting-started-guide/language-setup/automatic-redirect-based-on-browser-language/

So, if you set two different pages (one, the default language, and one the translation) where each page is loading a different form through a shortcode for example, there's no need for custom coding and you can still accomplish what you wanted.

I hope that you will find all this information helpful.

Mihai Apetrei

July 24, 2024 at 3:20 am #15995462

amandaL-9

Mihai,

Thank you for the response. It actually doesn't matter to me if I get the language from WPML or from the browser. I am using the auto-redirect based on the browser, but I don't want to have a ton of duplicate content. This form is on about 30 different pages, so this simple switcher is much easier than having duplicates.

We had originally written the code to work with Weglot and adapted it to WPML when we switched recently. The original code used $current_language instead of ICL_LANGUAGE_CODE, but that wasn't working either. The same code IS working fine on my Chinese site, just not on this one.

I'm wondering if there's something that should be translated and isn't? Given that the code works on another site and has worked fine in the past with another translation plugin, I'm hesitant to think that it's the issue.

Just to confirm, I did check to make sure we were pointing to the correct ACF fields and checked the language tags. Those are correct as far as I can tell.

Thank you

July 24, 2024 at 8:53 pm #16000454

Mihai Apetrei
WPML Supporter since 03/2018

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hi there, and welcome back.

I understand that you would prefer to go with this custom implementation route, and I also understand the reasons why this makes more sense for this specific case.

This will require custom coding, as I mentioned above, and it falls outside the scope of our support policy.

I would recommend hiring a WPML contractor (https://wpml.org/contractors) or a developer who can assist you with this implementation.