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.

This topic contains 9 replies, has 6 voices.

Last updated by maartenD-4 3 years, 6 months ago.

Assigned support staff: Yuri.

Author Posts
March 6, 2018 at 3:17 pm #1537825

zvonimirM

Hi

I have an intro page for the first time website visitors where they need to chose preferred language. Basically they can choose between 4 different languages, and problem is that there is no way to redirect automatically returning visitors to home page in language they previously chose.

I really don't know how to solve this - using that splash page as a home page is definitely not solution.

March 6, 2018 at 5:06 pm #1537972

Yuri

Hi, currently WPML does not provide a redirection option based a previous language selection. What you could do is use a plugin called Redirection together with WPML to configure this redirection based on a user's cookie. The following would need to be done:

- configure WPML to store the language cookie for AJAX calls in WPML > Languages in the "Language filtering for AJAX operations" configuration block. This will create a cookie named "_icl_current_language" in every visiting user's machine.
- configure the plugin Redirection (wordpress.org/plugins/redirection/) to check this cookie and redirect to the pages you want.

Take notice that this is only a guideline for what you can do and is untested. If this approach does not work you may need additional custom programming.

March 7, 2018 at 12:55 am #1538429

zvonimirM

Hi Jury

Thanks for helping me to solve this issue. I have problems with implementing cookies on a root page - every time cookie resets, so I have redirection to default language.

I have disabled browser redirection option on WPML, languages are in different folders and I have disabled other plugins that might cause that problem.

As I can read on support forum, there are a lots of problems with cookie reset - hoping that we'll solve this:).

March 7, 2018 at 10:40 am #1538839

Yuri

No problem. If you need any further help with this issue, just let us know here in the forum.

March 7, 2018 at 10:49 am #1538861

zvonimirM

Redirections works great - but only for default language... If first time visitor chose second language, cookie stores his language preference. But when he returns to root page, cookie resets and redirect visitor to default language homepage. Any idea how to solve this?

March 7, 2018 at 5:28 pm #1539553

Yuri

Hi, I believe that the solution for this will be to custom-code a cookie that will deal with the existing one and manage the redirection check. Unfortunately, this falls outside of the scope of our support here but you can try reaching one of our certified contractors. A comprehensive list can be found here: https://wpml.org/contractors/

February 19, 2019 at 8:28 am #3215416

Erwin

No need to hire a contractor for this.

Here is a simple code snippet you can add to your functions.php file to fix this annoying behavior, based on an excellent answer on Stack Overflow (https://stackoverflow.com/questions/7762573/how-to-automatically-jump-to-the-corresponding-language-of-the-page-using-wpml-p/54761486#54761486) which I have slightly improved:

add_action('init', 'my_icl_set_current_language');
add_action('wp_loaded', 'my_icl_set_current_language');

function my_icl_set_current_language() {
    global $sitepress;
    $default_lang = $sitepress->get_default_language(); //set the default language code
    $supported_lang = array_keys(icl_get_languages('skip_missing=0&orderby=code')); //set the allowed language codes 
    $get_lang = $default_lang;

    $langtemp = @$_COOKIE['lang'];
    if (in_array($langtemp, $supported_lang)) $get_lang = $langtemp;

    $langtemp = @$_GET['lang'];
    if (in_array($langtemp, $supported_lang)) $get_lang = $langtemp;

    if (in_array($get_lang, $supported_lang)) {
        //save cookie setting
        setcookie ('lang', $get_lang, time() + (10 * 365 * 24 * 60 * 60), '/');

        if ($sitepress->get_current_language() != $get_lang) {
            $sitepress->switch_lang($get_lang, true);
        }
    }
    define('CURRENT_LANGUAGE_CODE', $get_lang); //use this constant to check the current language code instead of ICL_LANGUAGE_CODE
}
February 13, 2020 at 1:23 pm #5486369

tomL-20

Sorry to disturb you both, but the code does not work with me.

I installed a plugin from github. Theme_Customisations to add to my functions.php. That way I am safe with future updates.

But when I implement your codes it stays on my default language 'Eng'. Whilst the url does change to the language directory changes (in the url). The language stays English.

Where there any updates on WPML that disabled your codes?

Thank you in advance.

June 9, 2020 at 9:35 am #6328973

tomB-16

I have found another way to remember a user's language choice. You can find the explanation on my blog: hidden link

/**
 * Add ?switch_lang=lang to all url's in the languaqe switcher
 */
add_filter( 'icl_ls_languages', function( $languages ){
    global $sitepress;
    foreach( $languages as $lang_code => $language ){
        $languages[$lang_code]['url'] = add_query_arg( array( 'switch_language' => $lang_code ), $languages[$lang_code]['url'] );
    }
    return $languages; 
} );
/**
 * Update wp-wpml_user_selected_language cookie when user switches language
 */
add_action( 'plugins_loaded', function (){
    // GET variables
    $switch_language = filter_input(INPUT_GET, 'switch_language', FILTER_SANITIZE_STRING);
    if( $switch_language ) {
        $languages = icl_get_languages();

        // Check if the switch_language variable is a registered language
        if( array_key_exists( $switch_language, $languages ) ) {
            // Create a cookie that never expires, technically it expires in 10 years
            setcookie( 'wp-wpml_user_selected_language', $switch_language, time() + (10 * 365 * 24 * 60 * 60), '/' );

            // Let's redirect the users to the request uri without the querystring, otherwise the server will send an uncached page
            wp_redirect( strtok( $_SERVER['REQUEST_URI'], '?' ) );
            exit;
        }
    }
}, 1);
/**
 * Switch language if the user has selected his language before
 */
add_action( 'pre_get_posts', function ( $query ) {
    // Disable for admin area & only for main query
    if( is_admin() || !$query->is_main_query() ) {
        return;
    }

    // Disable for logged in users
    if( is_user_logged_in() ) {
        return;
    }

    // Check if requested webpage is /
    if( strtok( $_SERVER['REQUEST_URI'], '?' ) != '/' ) {
        return;
    }

    // GET & COOKIE variables
    $switch_language           = filter_input( INPUT_GET, 'switch_language', FILTER_VALIDATE_BOOLEAN );
    $user_selected_language    = filter_input( INPUT_COOKIE, 'wp-wpml_user_selected_language', FILTER_SANITIZE_STRING );

    // If user has selected his language before & user did not just change his language
    if ( $user_selected_language && !$switch_language ) {
        do_action( 'wpml_switch_language', $user_selected_language );
    }
}, 1);
July 16, 2020 at 5:46 am #6608975

maartenD-4

This does not work

add_action( 'plugins_loaded')

hook does not load at all. I changed it to 'init' and now it's working without a problem