Overview of the Issue
When using RankMath SEO or Yoast SEO with our add-on WPML SEO, you might experience incorrect hreflang values in the sitemap. Instead of using the hreflang values set in WPML’s language settings, the sitemap takes the language code (e.g., nz, au) instead of the expected full code (e.g., en-nz, en-au).
Workaround
This issue is fixed in WPML SEO 2.2.3. Updating solves it on your site, so you only need this workaround if you can’t update yet.
Please, make sure of having a full site backup of your site before proceeding.
- Open the …/wp-content/plugins/wp-seo-multilingual/classes/Shared/Sitemap/BaseAlternateLangHooks.php file.
- Look for line 180.
- Replace:
private function getAlternateLinks( $alternateLangs ) { $buildAlternateLink = function ( $url, $lang ) { return '<xhtml:link rel="alternate" hreflang="' . esc_attr( $lang ) . '" href="' . esc_url( $url ) . '" />'; }; - With:
private function getAlternateLinks( $alternateLangs ) { $buildAlternateLink = function ( $url, $lang ) { $languages = apply_filters( 'wpml_active_languages', null, [] ); $hreflang = isset( $languages[ $lang ]['tag'] ) ? $languages[ $lang ]['tag'] : $lang; return '<xhtml:link rel="alternate" hreflang="' . esc_attr( $hreflang ) . '" href="' . esc_url( $url ) . '" />'; };