[Resolved] Contact Form Switch Based on Browser Language (PHP)
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.
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 );
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?
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.
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.
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.
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.
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.