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.

This topic contains 9 replies, has 2 voices.

Last updated by alexanderK-15 12 months ago.

Assigned support staff: Bruno.

Author Posts
October 14, 2018 at 4:58 am #2815081

alexanderK-15

Hello,

what is the way to get the selected value of the language switcher in the admin bar in the BACKEND in a third party plugin code.

ICL_LANGUAGE_CODE shows (correctly) the language when a specific language is being selected, but show the DEFAULT one when "All Languages" is selected. How to distinguish these two cases?

October 15, 2018 at 8:16 pm #2818764

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

Thank you for contacting us. Please, my sincere apologies for the delay in answering.

It depends on how you are inserting this code. I tested the code below and it worked correctly:

add_action( 'admin_init', function() {
	echo ICL_LANGUAGE_CODE;
});

Using the above code, does the problem persist? In my test environment, this returns "all" when this option is selected in the top menu.

Thank you.

October 16, 2018 at 10:01 pm #2822468

alexanderK-15

No, it doesn't work.
Please note that I'm trying to get the current language in the admin inside some other plugin. Your example works inside functions.php of the theme. Not the case inside a plugin. ICL_LANGUAGE_CODE is not empty which means your code was executed before its usage.
Once again - suppose there are two languages - EN, DE, your selector normally returns to ICL_LANGUAGE_CODE one of "en", "de", "all". However "all" doesn't work as expected, it is "de" which is the default language for the site.
I'm looking at your code in function get_cookie_lang() in sitepress-multilingual-cms/inc/request-handling/wpml-request.class.php
the latest line is:
$lang = $wpml_language_resolution->is_language_active( $lang ) ? $lang : $this->default_language;

and this is what causes the problem. In the described case it seems "all" passed to is_language_active() returns false for some reason.

Can you provide a resolution to this problem?

October 17, 2018 at 1:42 am #2822653

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

This for me works inside a plugin too. Here is a simple example:

<?php
/**
Plugin Name: Custom Plugin Test
*/

add_action( 'admin_init', function() {
	var_dump( ICL_LANGUAGE_CODE );
});

I'm not sure what you're trying to do, but I should suggest that you test this with just the above code and with WPML enabled (no other plugins or with some custom theme - use the default theme to test).

From the simple test I did above, this works for both the functions.php file of the theme and the plugin (see example above).

Please, note as this is a custom coding, unfortunately, this is beyond the scope of our support.

We have a list of WPML hooks that can help you develop such feature, which can be seen in the links below:

- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference

If you prefer, you can contact one of our certified partners who can develop small to large features for your site: http://wpml.org/documentation/support/wpml-contractors/

Thank you.

October 18, 2018 at 10:26 pm #2829764

alexanderK-15

Thanks. We are trying to use ICL_LANGUAGE_CODE inside a plugin, so to have different behavior depending on the currently selected language in the admin bar. For some reason the "all" value is treated by your function (pointed in my previous message) as invalid and then the last line of that function changes ICL_LANGUAGE_CODE to the default language.
One remark - the WPML plugin used in this site is a little old. It's possible that this is an old bug that you have fixed in the latest version. We will try to update it and test for ICL_LANGUAGE_CODE again and I will let you know if this would resolve the problem or not.

October 19, 2018 at 2:09 am #2829975

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Yes, that may have happened in some older version. After updating WPML and its add-ons, please let me know if the problem persists.

If the problem still persists, try a new installation to see the behavior of WPML and the simple plugin I posted above.

Thank you.

October 19, 2018 at 6:23 am #2830597

alexanderK-15

Hi,
just updated everything (successfully) and did the test with the same result.

2. Also did the following - a fresh install of the latest version of everything. Sam result.

BUT I discovered what is happening - the problematic thing is an ajax request. It seems that there is no way to achieve what I want (is it) - the ajax request is not treated as is being in the admin panel and so there the "all" option for languages is not allowed and this is why you substitute it with the default language. Just note that the "all" is the value of ICL_LANGUAGE_CODE just up to the problematic line of sitepress-multilingual-cms/inc/request-handling/wpml-request.class.php (so it correctly reads it from the cookie).

So the last question is - do you know a way to set up somehow an ajax call that will work and would set ICL_LANGUAGE_CODE to "all" when it is in the cookie

October 20, 2018 at 12:49 am #2833239

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello,

Since you are using AJAX, you may need to try to pass some argument within the script you are using. For example, before using the language code inside an ajax script, you should pass this as an argument (still in JavaScript).

After that, you may be able to access the language code via AJAX. I'm not sure if this should work as expected, but that would be an option.

Thank you.

October 20, 2018 at 1:15 am #2833282

alexanderK-15

Yes this is the way we solved, too.

October 20, 2018 at 1:16 am #2833283

alexanderK-15

My issue is resolved now. Thank you!