Add HREFLANG X-Default Tag to WPML Multilingual Site

WPML multilingual plugin for WordPress can already add hreflang tags for your languages used. I like to add x-default language tags too to designate default version to search engines. Here’s how it could be added to your child theme, when default language is English.

Put this in functions.php of your theme.

/* ---------------------------------------------------------------------------
* Set hreflang="x-default" according to Google content guidelines with WPML
* --------------------------------------------------------------------------- */
add_filter('wpml_alternate_hreflang', 'wps_head_hreflang_xdefault', 10, 2);
function wps_head_hreflang_xdefault($url, $lang_code) {
if($lang_code == apply_filters('wpml_default_language', NULL )) {
echo '<link rel="alternate" hreflang="x-default" href="' . $url . '" />';
}
return $url;
}

 

Share:
This entry was posted in WordPress and tagged . Bookmark the permalink.

One Response to Add HREFLANG X-Default Tag to WPML Multilingual Site

  1. maniek says:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘wps_head_hreflang_xdefault’ not found or invalid function name

Leave a Reply

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