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.

Sun Mon Tue Wed Thu Fri Sat
- - 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00
- - - - - - -

Supporter timezone: America/Lima (GMT-05:00)

This topic contains 11 replies, has 1 voice.

Last updated by Andreas W. 6 days, 20 hours ago.

Assisted by: Andreas W..

Author Posts
February 16, 2025 at 10:37 am #16710374

desireeM

Background of the issue:
https://wpml.org/documentation/getting-started-guide/language-setup/enabling-language-cookie-to-support-ajax-filtering/
language-cookie.js

This makes no sense and doesn't work.
If using languages in directories where the language cookie was set in a sub-path, it won't unset it either, since it has '/' hardcoded as path, but when you set it, you use COOKIEPATH constant (CookieLanguage.php line 66)

For whatever arbitrary reason you decided that if the user isn't logged in, it will load that JS (CookieLanguage.php), which effectively breaks things when page caches are used (since you can also page cache when users are logged in, any dynamic parts can be e.g. ajax loaded or with subrequests)

Also why do you need a cookie in the first place? You can infer the language from the domain (or subdirectory if language in directories) in the AJAX request anyway.

Symptoms:

Questions:
Why is the path hardcoded as '/' instead of using COOKIEPATH?
How is this supposed to work with page caches?
Why is a cookie needed if the language can be inferred from the domain or subdirectory?
Can you perhaps provide 1 example of a plugin action where the cookie of https://wpml.org/documentation/getting-started-guide/language-setup/enabling-language-cookie-to-support-ajax-filtering/ is necessary and it won't work otherwise?

February 16, 2025 at 10:39 am #16710378

desireeM

Bc right now the JS will only remove the cookie, not set anything, despite even your code comments being aware of that issue.
wpml-request.class.php:

	 * When user is not logged we must set cookie with JS to avoid issues with cached pages
	 *
	 * @param string $lang_code
	 */
	public function set_language_cookie( $lang_code ) {

Which is right, the issue is just that the JS doesn't do what it's meant to do?

February 16, 2025 at 10:41 am #16710380

desireeM

In your code there is an issue link @see hidden link however that's not publically accessible, could you perhaps let me know the details in there?

February 20, 2025 at 8:40 pm #16731316

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Our apologies for the delay in answering your questions.

Why is the path hardcoded as '/' instead of using COOKIEPATH?
- The code uses COOKIEPATH if defined and uses "/" as fallback

$cookie_path   = defined( 'COOKIEPATH' ) ? COOKIEPATH : '/';

How is this supposed to work with page caches?
- Setting page caches for non-logged-in users is generally advisable for performance reasons, but it needs to be done with caution when dealing with dynamic content (like multilingual websites, user-specific data, etc.). Our support can not provide more details on such a topic, as this is more of a question for developers. If you suspect a bug in WPML we can offer to try to recreate and escalate it.

Why is a cookie needed if the language can be inferred from the domain or subdirectory?
- We must set this cookie for non-logged-in users, as WordPress has at this state no user information to serve the requests per language. Without it, even if the language is inferred from the URL, dynamic interactions (like AJAX) would lose the language setting.

---

Could you please clarify:
1) In which cases does setting the filter not work on your site?
2) How can we recreate such an issue? Please provide detailed steps.

I will be glad to provide a WPML test site for this purpose, in case there is something we might need to try to recreate and escalate internally.

Best regards
Andreas

February 21, 2025 at 2:16 pm #16734052

desireeM

>- The code uses COOKIEPATH if defined and uses "/" as fallback

It does not, only at that place I mentioned and you copied. Look at class-wpml-cookie-scripts.php line 41

'path'    => '/',

>How is this supposed to work with page caches?

You misunderstood. I mean your current code does not work, when page cache also caches pages for logged-in users. However the whole purpose of that cookie language JS is to make it work correctly for page cached pages in the first place - at least that is what it says in your code comments and info page.

>Without it, even if the language is inferred from the URL, dynamic interactions (like AJAX) would lose the language setting.

Why? How would an AJAX request be any different from a non-AJAX request in terms of request URL? If you're able to infer the language from the URL in non-AJAX requests, you can do so for AJAX requests too. Or what specifically is the technical reason that you think you can't?

February 21, 2025 at 5:37 pm #16734734

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I consulted ChatGPT about your assumption letting it validate the code and received the following answer:

Considerations with Language Directory Structure:

Language Cookie Path:

The cookie is set with the path /, meaning it applies across the entire domain (including subdirectories). This is important when you have a language directory structure like sitename.com/de because the cookie will be accessible across all language versions of the site.
This should work as expected because a cookie with a / path is valid for all URLs on the site, including sitename.com, sitename.com/de, sitename.com/fr, etc.

Further steps:

If this does not work as expected on your site, and you suspect a bug, I can offer a WPML test site on which you can replicate the issue and we can take further steps from there.

It might be advisable that you test your site in a minimal setup first before we take steps in trying to recreate the issue.

I can not escalate anything in this case unless the issue is replicable on a test site in a minimal setup.

February 23, 2025 at 8:57 am #16737406

desireeM

ChatGPT is wrong. If you have a cookie with path /foo/ then setting a cookie with path / will not overwrite the cookie at /foo/

February 25, 2025 at 3:50 pm #16746822

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

It should use COOKIEPATH by default and if COOKIEPATH is not defined it will use "/".

You might need to define COOKIEPATH in your code.

I can offer a WPML test site on which you can recreate the issue and we take a closer look into this.

Would you be available to cooperate on this matter?

March 4, 2025 at 7:35 pm #16775752

desireeM

>It should use COOKIEPATH by default and if COOKIEPATH is not defined it will use "/".

It does not. I even provided you the code snippet showing you that it doesn't in https://wpml.org/forums/topic/language-cookie-js-code-makes-no-sense/#post-16734052 ?

March 4, 2025 at 9:06 pm #16775986

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Any issue that we escalate internally needs to be recreated first.

Please recreate the issue on the following test site and leave me a comment once you are done with this task.

One-Click-Login:
hidden link

March 5, 2025 at 2:22 pm #16779401

desireeM

Honestly, I just had a reply and submitted it, just to get an error:
>Error: Are you sure you wanted to do that?

And my reply is gone. What a waste of time.

Anyway:
What exactly?
Can you please reply my questions first, bc you didn't answer a single one of them yet.

1) Why is the path hardcoded as '/' instead of using COOKIEPATH? See https://wpml.org/forums/topic/language-cookie-js-code-makes-no-sense/#post-16734052 where I posted the code snippet showing that COOKIEPATH isn't used

2) How is this supposed to work with page caches?
Enable a page cache that serves cached frontend pages to logged in users (since the page doesn't have any dynamic elements anyway usually). Then the language cookie will always get removed by the JS for that user.

3) Why is a cookie needed if the language can be inferred from the domain or subdirectory?
Can you perhaps provide 1 example of a plugin action where the cookie of https://wpml.org/documentation/getting-started-guide/language-setup/enabling-language-cookie-to-support-ajax-filtering/ is necessary and it won't work otherwise?

March 5, 2025 at 7:06 pm #16780398

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I can not simply open a development ticket and suggest that the code might be wrong, without having an example that actually proves it.

Further, your questions are development-related and out of scope for support. I honestly do not have an answer to those.

I can consult our development about the details, but to handle support requests I first need to understand what the actual issue on your website is and try to recreate the issue.

Further, bugs need to be recreated on a test site in a minimal setup before being able to be escalated.

Also, we need to know how your code looks, as it sounds as if you are using custom code to set the cookie.

Please revise the following WPML test site which has a directory for default language and the AJAX filtering enabled. The cookie is set as expected. It might be confusing that the dev tolls show the path as "/" but the fact is that the cookie shows up in the application and it works as expected. See screenshot.

Sandbox:
hidden link

If this does not work as expected on your site, then this might be related to a third plugin or to custom code. I would need in this case exact details about the issue and any custom code used. You can use the above-mentioned sandbox to recreate such an issue.

cookie.jpg