WordPress Multilanguage Plugin and automatic redirects

WPML is one of the most popular plugins to build your typical multi-language website.

One of its most used features, is the browser’s language redirect, so someone with a browser in German, is properly sent to the german version of the site.

However, this feature has a big problem: the site will load twice if the main language for the homepage is other than the browser’s, because the redirect is made with javascript and it takes time to process (user-side technology), resulting in a very bad user experience.

After browsing the WPML forums, I saw they plan to fix this in the future, but meanwhile I came up with a solution myself to implement in one of my clients’ sites.

Step 1

First, let’s disable the browser’s automatic language redirect that WPML provides (in its settings).

Step 2

Then, we’ll open header.php, and BEFORE any other code, we’ll paste this.

Change the languages according to your project.

Here we go:

 Step 3

If you’re using W3 Total cache or other cache plugins, it is recommended that you include the code in a separate file, in header.php, like so:

server-side-languages-redirect.php would contain the provided code, and would be placed in your theme’s folder to be correctly included.

Then go to the settings of you Cache Plugin, and exclude that template from the caching process.

Example, in W3 Total Cache go to Performance > Page cache – scroll down and find “Never cache the following pages:”, then include this: wp-content/themes/YOURTHEME/server-side-languages-redirect.php, replacing YOURTHEME by your theme’s name.

EDIT: W3TC seems to be caching the file even if we protect it. So I opted provisionally for letting the homepage uncached (there’s a checkbox for that on Page Cache). Will update with news if I find how to improve it!

EDIT 2 November 2016: W3TC works fine with this solution as of version without creating an exception at all for the custom file – so go ahead and test it, and if you find any trouble don’t forget to comment! Cheers,

3 people super-liked this.

4 thoughts on “WordPress Multilanguage Plugin and automatic redirects

  1. Hi, were there any developments since you posted this? Is W3TC still caching your file even though you excluded it?

  2. Hi Matthias we are using this system with W3TC and WP 4.6.1 and it works without creating an exception at all so yes it should work! Cheers,

Leave a Reply

Your email address will not be published. Required fields are marked *