Please make sure to update to WPML 4.3.6 and check our list of Known Issues before reporting

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

Last updated by Bruno Kos 1 month, 1 week ago.

Assigned support staff: Bruno Kos.

Author Posts
December 4, 2019 at 11:00 am #5067525

amitP-2

I am trying to: hook into changing language function to disable server-side redirect if the user selects a language

Link to a page where the issue can be seen: hidden link

I expected to see:
currently, I have the following function in function.php:
//set cookie to stop redirecting when user select language.
function stop_redirect() {
// These actions will be executed right after the global current language has just changed.
setcookie ('redirect_enable', 'false', time() + (60*60*24*7), '/');
}

add_action( 'wpml_language_has_switched', 'stop_redirect' );

I expect the cookie to be created ONLY when language has been switched by the user.

Instead, the cookie is set on every page load.

December 4, 2019 at 11:55 am #5067973

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

This has been escalated to our 2nd tier team and may take some debugging time, I'll get back to you as soon as I have any news or questions for you.

Regards,
Bruno Kos

December 5, 2019 at 10:16 am #5075159

amitP-2

Is there any update on this?

December 5, 2019 at 10:18 am #5075167

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

I am still discussing this with 2nd tier, may have some info today, will update you.

Regards,
Bruno Kos

December 5, 2019 at 2:05 pm #5077291

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

This action does not trigger when the user switched the language, it only triggers when you switch the language in code using function switch_lang() or similar functions.

Does it help? Something in those lines:
https://wpml.org/forums/topic/wpml_language_has_switched-runs-three-times-through-all-langs/#post-1223785

Regards,
Bruno Kos

December 5, 2019 at 2:51 pm #5077611

amitP-2

Unfortunately, this doesn't help me.
Let me provide you with what we want to do, so you may provide a different workaround:
We currently using our server (Nginx) GeoIP rules to redirect customers to the correct language according to their IP.
We still want to allow our customers to switch to different languages if they want, so our server checks for a cookie to disable the redirects.
I was looking for a way to know when the customer changed his language so we can set the cookie at that time.

December 6, 2019 at 8:20 am #5081759

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

I was looking for a way to know when the customer changed his language so we can set the cookie at that time.

If using WooCommerce multilingual, what about this then?
https://wpml.org/forums/topic/language-switch-hook-is-not-triggered/#post-2821360

Regards,
Bruno Kos

December 7, 2019 at 12:09 pm #5087339

amitP-2

Hi,
Unfortunately I don't use woocommerce.
I was looking even for a JavaScript solution, but using menu switcher doesn't provide a unique ID to bind an event to.
Is there any other possible solutions?

December 9, 2019 at 7:02 am #5090345

Bruno Kos
Supporter

Languages: English (English )

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

Hi,

I see - in this case, the only possible solution could be using the following:
https://wpml.org/wpml-hook/wpml_language_has_switched/, along with https://wpml.org/wpml-hook/wpml_switch_language/

However since I don't have any working examples to show you, apart from what our documentation says, and given the fact that it is a rare custom request ( I actually found almost no examples of using these functions in our archives), I suggest you try reaching out our contractors and see if they can assist with this type of development, there isn't anything else at this point I can suggest.

I also didn't find many examples in Github, only something like this:
hidden link

Regards,
Bruno Kos