开发者

How to Automatically jump to the corresponding language of the page using WPML plugin in wordpress

开发者 https://www.devze.com 2023-04-12 18:09 出处:网络
I\'m setting up a multi-language blog using WPML. I works well using the button to change the language.

I'm setting up a multi-language blog using WPML. I works well using the button to change the language. Is there any way t开发者_StackOverflow社区o automatically jump according to the visitor's language?


To improve on the excellent answer by Seh Horng, here is the same code to add to your functions.php, except you don't need to manually set $default_lang and $supported_lang (they are retrieved automatically):

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
}


If you are looking to customize WPML to allow remember of last language preference selected by your visitor, you can place the following codes in WP functions.php:

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

function my_icl_set_current_language() {

    $default_lang = 'en'; //set the default language code
    $supported_lang = array('zh-hans', 'en'); //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), '/');

        global $sitepress;
        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

}

With this customization, the visitor only needs to access http://www.yoursite.com/?lang=zh-hans once then it will remember the preference and all future requested pages should show the selected language i.e. Simplified Chinese accordingly unless other language has been selected or the lang cookie has expired.


We use WPML 2.4.1 (Multilingual CMS version) and it includes this functionality.

WPML->Languages->Browser language redirect

Here is what the on-screen documentation has to say:

WPML can automatically redirect visitors according to browser language.

Important notes:

This feature should never be used when caching plugins are enabled. Visitors who have cookies disabled may have trouble switching between languages.

  • Disable browser language redirect

  • Redirect visitors based on browser language only if translations exist

  • Always redirect visitors based on browser language (redirect to home page if translations are missing)

Remember visitors' language preference for 24 hours.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号