How to make sure the correct domain is used in WPML

If you use the WPML multilingual plugin for WordPress, and have each language on a different domain, you may have noticed that WPML does not ensure that all URLs in your page point to the correct domain. You might notice, for example, that images are pulled from the main domain, even when you’re accessing a page on another language’s domain. It’s rare that this causes problems for your site, (although it theoretically could if you have .htaccess rules in place to stop hot-linking, or if you have enabled https for one domain and not for the others), but it’s likely to have a small negative impact on your SEO.

If you want to fix this, you need to filter all of the WordPress functions that return a URL. There are lots of them, and I haven’t collected them all together. But there’s enough here to give you the picture, and if you find something is not being filtered it should be easy enough to find the appropriate function and add it to the code.

//Add all the necessary filters. There are a LOT of WordPress functions, and you may need to add more filters for your site.
add_filter ('site_url', 'wpcs_correct_domain_in_url');
add_filter ('get_option_siteurl', 'wpcs_correct_domain_in_url');
add_filter ('stylesheet_directory_uri', 'wpcs_correct_domain_in_url');
add_filter ('template_directory_uri', 'wpcs_correct_domain_in_url');
add_filter ('post_thumbnail_html', 'wpcs_correct_domain_in_url');
add_filter ('plugins_url', 'wpcs_correct_domain_in_url');

/**
* Changes the domain for a URL so it has the correct domain for the current language
* Designed to be used by various filters
* 
* @param string $url
* @return string
*/
function wpcs_correct_domain_in_url($url){
    if (function_exists('icl_get_home_url')) {
        // Use the language switcher object, because that contains WPML settings, and it's available globally
        global $icl_language_switcher;
        // Only make the change if we're using the languages-per-domain option
        if (isset($icl_language_switcher->settings['language_negotiation_type']) && $icl_language_switcher->settings['language_negotiation_type'] == 2)
            return str_replace(untrailingslashit(get_option('home')), untrailingslashit(icl_get_home_url()), $url);
    }
    return $url;
}

Comments

  1. Would you recommend WPML as the best way to translate a website to different languages? I’ve used google translate code snippets to date on http://www.world-outreach.com as find that the best way to present different translations and it’s free but it does involve alot of manual coding on each page. There are different views on the best way to translate a site, eg http://www.netmagazine.com/tutorials/build-multilingual-site-wordpress and WPML does seem the best way to go, I’ve tried using multisite but maintaining different sites with an mu installation isn’t the easiest thing to do! Paul

  2. Thanks for solution, it helped me a lot!
    Can you suggest how to add filters to different images that theme cuts, so they have right domain name too.

    For example template cut image on uploading:
    $config['imgSize']['featured'] = array(‘width’=>990, ‘height’=>400 );

    BR,
    Aleksandr

  3. I found it by myself, filter for images:
    add_filter (‘wp_get_attachment_url’, ‘wpcs_correct_domain_in_url’);

    Thanks,
    Aleksandr

Speak Your Mind

*