0

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.

3
  • 2
    Have you tried a multisite and MultilingualPress?
    – fuxia
    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
    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
    May 25, 2015 at 12:10

2 Answers 2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.