Skip Navigation

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

Problem:
The client wants to get the current language using the wpml_current_language hook but it was not working.

Solution:
There was a logical error in the client code, either use the global keyword to access the variable that is defined outside of the function definition or use the wpml_current_language filter inside the function.

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

Last updated by autumnW 1 year, 11 months ago.

Assisted by: Noman.

Author Posts
May 22, 2023 at 1:38 pm #13690451

autumnW

Tell us what you are trying to do?
Im trying to make and if statement that checks the current langauge and then executes code depending on the language detected.

Is there any documentation that you are following?
I followed the documentation provided, https://wpml.org/faq/how-to-get-current-language-with-wpml/#:~:text=Getting%20the%20Current%20Language%20of%20your%20Site&text=function%20get_language_shortcode()%20%7B%20return%20apply_filters,code%20of%20the%20current%20language.

Is there a similar example that we can see?

What is the link to your site?

Screenshot 2023-05-22 at 15.37.16.png
May 22, 2023 at 1:57 pm #13690715

autumnW

Tried changing a few things but still have no luck, any advice would be appreciated

Screenshot 2023-05-22 at 15.56.21.png
May 23, 2023 at 9:51 am #13696633

autumnW

Hi, Could anyone let me know if i am calling this wpml function correctly, I just need to know how to structure the if condition to validate the current language

May 23, 2023 at 11:32 am #13698163

Noman
WPML Supporter since 06/2016

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting WPML Support. It seems you are using the wpml_current_language filter outside of the function that’s why it’s not accessible inside the function, so either move that filter to inside the function or use the global keyword inside the function something like this:

global $current_lang;

I hope it will solve your issue.

Thank you

it should be used inside function.png
May 23, 2023 at 12:27 pm #13699065

autumnW

Thank you very much Noman, very simple fix.

Im was unaware of the global prefix to call variables outside the scope of a function, im used to python.

Thanks a lot for you help 🙂