0

I'm creating a website for a company that has multiple countries open and different websites for each.

I want to create a landing page with the flags for each available country or a "global" site. I would like it to ask the user only the first time he accesses and remember the country he selects.

When the users clicks for example on the Colombian flag, it redirects to co.mysite.com

If the user clicks on the Mexican flag, it redirects to mx.mysite.com

The next time user visits, it remembers the choice.

1
  • sorry, but this is a generic web design question that most likely better be asked on SO, and is really off-topic here (the right answer is to have the users register and select by themselves what site they want to be redirected to, but it also a stupid answer as user can just bookmark the page) Jun 7, 2017 at 3:29

1 Answer 1

2

I recently had to implement a similar solution and decided to build it from the ground up. It is quite an involved task and if you are not sure how to do it, I would suggest you try a plugin first (a quick search on the Wordpress plugins site had few options actually but there appears to be a commercially available on on CodeCanyon called Wordpress Country Selector).

It is difficult to give you a specific answer to such a large question without knowing your exact site set-up, but here's the approach I'd take:

  • Identify your user's country/language based on their browser settings. You need this logic very early in the Wordpress flow, so I'd suggest doing it in your functions.php and hooking in to something like the after_setup_theme hook.
  • Redirect them (using Wordpress custom redirects, using add_rewrite_rules on the init hook) to your appropriate site version. Assuming you are serving localised content from one Wordpress site rather than from a Wordpress multisite setup, I'd recommend you use a http://example.com/xx structure rather than a subdomain structure such as http://xx.example.com/.
  • Save the preference in a cookie so you don't have to do the auto-detect next time.
  • Then create an interface that enables the user to make the same choices manually. This may be a visual 'flag' based interface if you prefer (or a dropdown etc).
  • As above, if the user manually selects a different country/language, use that to over-ride the auto-detected cookie and assume their manual choice takes priority over your auto detection logic (as there is a chance of browser settings be different to the user's explicit manual choice).
  • At that point, you now know which language/country your user wants. Now the fun starts when you try to decide the many different ways you are going to provide localised content to the user. Again, assuming you are serving everything from one Wordpress site, my approach is to use a Custom Post Type per language with custom Wordpress rewriting as, if planned from the outset, it enables you to handle localised URLs very nicely, but this is a very open topic with many possible solutions.

This is not the only way to do this I am sure, but I hope that helps provide some guidance based on an approach I recently implemented to this common scenario.

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.