Incorrect hreflang

In the Version 20.8.3 - January 27, 2018, this feature was added to your change log:
Added: Hreflang Tag in Header - The same as HTML lang code

The code change for that is in functions/theme-head.php

// hreflang | only if WMPL is not active
if( ! function_exists( 'icl_object_id' ) ){
$format_locale = strtolower( str_replace( '_', '-', get_locale() ) );
echo '<link rel="alternate" hreflang="'. $format_locale .'" href="'. get_permalink() .'" />'."\n";
}

The problem with this is it forces it to lower case, which for a us site means: en_us
According to google's guidelines: 
https://support.google.com/webmasters/answer/189077?hl=en , the ISO standard is upper case country, which would be en_US in that example.  

Wordpress adds the proper ISO code, so the page HTML tag is:
<html lang="en-US"

So when BeTheme then adds <a linkref="en_us", this results in a mismatch and causes errors to be reported from Google's SEO tools.

The workaround for this currently is to add a function like this to the BeTheme child theme to trick it into thinking WPML is installed.
//Define the WMPL function to trick BeTheme into thinking the plugin is installed so it stops adding hreflang
function icl_object_id(){
}

To fix this, I would suggest:
- Make adding the hreflang attribute optional in BeTheme settings.  99% of WordPress sites are single language, and don't want any hreflang tag automatically added to their links
- If you must add a hreflang attribute, at least make it match the ISO standard google is expecting.

Comments

Sign In or Register to comment.