Archives for July 2012

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;
}