Fixing WPML bug with different domains

Fixing WPML bug with different domains

Fixing WPML bug with different domains 150 150 La boîte à coder

As you may know, WPML offers the option to use different domains for different languages. It turned out that I already had the same domain name with different extensions: .com and .fr. So I decided to use that WPML feature.

After following these instructions, I had everything working fine except for one thing: the home page always redirected to the default language.

At first I thought it was something I didn’t do right but I finally found someone who had the same issue as me. I tried the provided solution but it didn’t work for me. It looks like to be a bug in WPML and they won’t admit it.

So I made my own fix! It’s not really pretty, if you have a better solution, please post it in the comments.

What I am doing is adding a filter on redirects on the home page. If the original URL is a secondary language domain, I abort the redirect by returning false.

This is what you can add in the functions.php in your child theme (just replace https://benjamindurin.com with your secondary language domain):

add_filter('redirect_canonical', 'homeUrlFilter', 10, 4);
function homeUrlFilter($redirect_url, $requested_url)
{
    if ($requested_url == 'https://benjamindurin.com') {
    	return false;
    }
    return $redirect_url;
}

Privacy Preferences

When you visit our website, it may store information through your browser from specific services, usually in the form of cookies. Here you can change your Privacy preferences. It is worth noting that blocking some types of cookies may impact your experience on our website and the services we are able to offer.

Our website uses cookies, mainly from 3rd party services. Define your Privacy Preferences and/or agree to our use of cookies.