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

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

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

Last updated by bomannN 9 months, 2 weeks ago.

Assigned support staff: Lauren.

Author Posts
February 20, 2019 at 4:43 am #3219905

bomannN

I have a multisite setup.I display various settings for each blog in a custom frontend dashboard view where the client can make changes. One of the request has been to set a default language for a blog.

I am saving this language code as part of a blog option (not required)
update_blog_option( $id, 'site_language', $language );

global $sitepress;
$sitepress->set_default_language($language);

I have made sure that the language codes are same as that being used by WPML but haven't been able to get this to work. I want to set the default language per site. So when the site(url) is opened, the first thing they see is the default language. Also i do not have a language switcher in the UI to prevent users from seeing it in another language.

I have to refresh the page twice to see the strings reflect the language change.
Am I missing something?

add_action('init', 'my_icl_set_current_language');
function my_icl_set_current_language() {
    global $sitepress;
    $language_code = get_blog_option( get_current_blog_id(), 'user_language' );
 
    if($language_code != ''){
        $sitepress->set_default_language($language_code);
        $sitepress->switch_lang($language_code);
    } else {
        $sitepress->set_default_language('nb');
        $sitepress->switch_lang('nb');
    }
     
}
February 20, 2019 at 3:18 pm #3222506

Lauren
Supporter

Languages: English (English )

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

Thank you for contacting WPML support. I'll be happy to help you with this.

Within WPML settings there is the option to set the default language. As long as browser redirect is not enabled, you would then have the behavior that you outlined above. So, in WPML -> Languages, set the language that you want as default and then scroll down that same settings page and make sure browser redirect is not enabled.

Please let me know if you need any further assistance and I will be happy to help.

February 21, 2019 at 5:34 am #3224993

bomannN

Hey Lauren,
Thank you but that did not solve my issue. I've been trying some more with my code.
"after_setup_theme" hook seems to be working in only some cases. I did come across "wp_loaded" hook which seems to work better. I have a few roadblocks though.

Hence I have decided to ignore this approach entirely, and base language of the site based on user's language instead.

I am referring this thread https://wpml.org/forums/topic/set-users-default-language-using-php/

The person has mentioned the following in one of his comments:
"Once the language has been set correctly, it's no longer possible to use the language switcher to change to a different language as on every page load the lang is reset to the default".

My scenario is that when user logs in, it will be in the language(X) he selected (pre-defined in user_meta). Now if he wants to change this language to another language(Y) from the language switcher, does he now have to switch every time to that language(Y) as he navigates across different pages?
Because if I use the "wp_loaded" hook, it will keep resetting the user's language(X) saved in user_meta.
How do I avoid this?

February 21, 2019 at 2:28 pm #3227998

Lauren
Supporter

Languages: English (English )

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

The best way to achieve what you are explaining is to enable browser redirect. This will automatically redirect the user to the language that his/her browser is set to, but also still allow the user to use the language switcher to change languages if they would like to. No need for custom programming for this scenario.

You can enable this in WPML -> Languages -> Browser Redirect settings.

February 25, 2019 at 5:00 am #3238008

bomannN

I have already done that, but the issue still persists. Isn't the language switcher supposed to reload the same page with the language rather than redirecting me to the home URL instead?

February 25, 2019 at 2:38 pm #3240336

Lauren
Supporter

Languages: English (English )

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

Yes, it is, as long as the translated page exists. Can you tell me which page you are seeing this behavior on? Also, if you add a language switcher to the footer or a sidebar, do you see the correct behaviour?

February 26, 2019 at 9:52 am #3243960

bomannN

The issue was on sandbox but worked on staging. So hopefully it works on live.