admin管理员组

文章数量:1122846

We are running a wordpress site with Greek content (political & economical analysis) and we want to set up an English site of our site.

The English site will be running on its own wordpress installation in a subdomain of the main site. If the primary site is then English site will .

My problem that I face is that I can not find a proper solution for the language switcher. Ideally I want a language switcher that recognizes the browser language and redirects accordingly and stores the language in cookie and if the user selects the other site to store that selection.

For example if I visit the main site by entering the url analyst.gr (or following some link) and landing on the main homepage and then switch language , effectively going to en.analyst.gr I would like to store this info so the next time I visit analyst.gr it will redirect me automatically to en.analyst.gr

I tried using WPML but it assumes that different languages go to the same wp installation using vhosts and at the moment the mapping of posts and menus is not needed.

We are running a wordpress site with Greek content (political & economical analysis) and we want to set up an English site of our site.

The English site will be running on its own wordpress installation in a subdomain of the main site. If the primary site is http://www.analyst.gr then English site will http://en.analyst.gr.

My problem that I face is that I can not find a proper solution for the language switcher. Ideally I want a language switcher that recognizes the browser language and redirects accordingly and stores the language in cookie and if the user selects the other site to store that selection.

For example if I visit the main site by entering the url analyst.gr (or following some link) and landing on the main homepage and then switch language , effectively going to en.analyst.gr I would like to store this info so the next time I visit analyst.gr it will redirect me automatically to en.analyst.gr

I tried using WPML but it assumes that different languages go to the same wp installation using vhosts and at the moment the mapping of posts and menus is not needed.

Share Improve this question edited Oct 10, 2016 at 8:02 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 25, 2015 at 11:44 Peter_mjtPeter_mjt 172 bronze badges 3
  • 2 Have you tried a multisite and MultilingualPress? – fuxia Commented May 25, 2015 at 11:46
  • I have seen it but needs the main site to be multisite which is not at the moment and we prefer to mess with it, and the plugin is oriented in translating posts which at the moment is not needed. But I will keep it in mind. – Peter_mjt Commented May 25, 2015 at 11:56
  • 1 You don’t have to translate any posts or terms in MultilingualPress. There are three available language switchers: a widget, nav menu integration, or custom code. Redirection is built-in. – fuxia Commented May 25, 2015 at 12:10
Add a comment  | 

2 Answers 2

Reset to default 0

To use Language Switcher for MULTI-SITE wordpress, then i advice several plugins:

1) MultiLanguage Site Framework - has language/flags switcher in top and above posts too, plus other options..

2) MultiSite language switcher - adds just switcher for flags/languages.

From your question I gather that you want three things:

  1. Read a cookie to see the user's preferred language.
  2. If there is no cookie, detect the browser language to see what the user's preferred language is.
  3. Based on the preferred language redirect the user either to the greek or english site.

There are various ways to handle cookies in WordPress. You could start looking here. You could store a language code in the cookie and retrieve it, storing it in the variable $wpse189348_lang. Detecting the browser language is easily done with this code:

$wpse189348_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// $wpse189348_lang will end up as 'en' or 'fr' or some other language code, but may be empty

This approach is not foolproof as you can see here. Now you have the language code in $wpse189348_lang you can use this to redirect users from the international to the greek site ('el' being the language code for greek).

add_action ('after_setup_theme', wpse189348_lang_redirect);
function wpse189348_lang_redirect {
  // read cookie and/or browser language here to get $wpse189348_lang
  if ('el' == $wpse189348_lang) {
    wp_redirect( 'http://www.analyst.gr' );
    exit;
    }
  }

Since the international site can have many different language codes you would use a negative test for redirecting the other way arount:

  if ('el' !== $wpse189348_lang) {
    wp_redirect( 'http://en.analyst.gr' );
    exit;
    }

Beware this is a fairly crude way of redirecting. Anything more subtle would require a plugin or extensive work.

本文标签: functionsLanguage switcher for subdomains