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

WPML and Autoptimize Inline Full CSS Problem

I’ve noticed that when I inline all CSS with Autoptimize plugin WPML code could be pushed to the bottom, even when I select output of hreflang tags to the top in WPML settings.

This code snippet could move your inlined CSS just before the closing </head> so that hreflang will go to top of source code.

// autoptimize before head
add_filter('autoptimize_filter_css_replacetag','te_css_replacetag',10, 1 );
function te_css_replacetag($replacetag) {
return array("</head>","before");
}

This way hreflang tags will have better visibility to google.

 

 

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

2 Responses 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 *