Overview of the Issue
When using Yoast SEO with WPML SEO, you might experience an issue where hreflang tags for hidden languages still appear in the sitemap’s source code as alternate language references, even though the posts in hidden languages are correctly excluded from the sitemap index.
Workaround
This issue is fixed in WPML SEO 2.2.2. 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 the function
addAlternateLangDatastarting at line 59. - After this line:
list( $elements, $getPermalink, $isIndexable ) = $this->getEntryHelpers( $type, $obj );
- Insert the following block of code:
// WPML Workaround for wpmlwpseo-350 - Exclude hidden language posts global $sitepress; $hidden_languages = $sitepress->get_setting( 'hidden_languages', [] ); if ( $elements && !empty($hidden_languages) ) { foreach( $elements as $lang => $element) { if ( in_array( $lang, $hidden_languages ) ){ unset( $elements[$lang] ); } } }