This thread is resolved. Here is a description of the problem and solution.
Problem:
The client reported that on some pages of their multilingual WordPress site, the x-default hreflang tag is missing. This issue occurs because some posts exist only in languages other than the default language (English UK), and there is no content available in the default language.
Solution:
We advised the client to ensure that posts/pages are initially created in the site's default language and then translated into other languages. This setup is crucial for the correct generation of hreflang tags, including x-default. If a post is missing in the default language, the x-default will also be missing. For posts that exist only in other languages, we provided a custom solution to manually add an x-default hreflang tag. The client can add the following code snippet to their theme's functions.php file:
add_filter('wpml_hreflangs', function($hreflang_items) { if (empty($hreflang_items['x-default']) && is_singular()) { $post_type = 'post_' + get_post_type(); $translations = apply_filters('wpml_get_element_translations', [], apply_filters('wpml_element_trid', false, get_the_ID(), $post_type), $post_type); if (!empty($translations)) { foreach ($translations as $lang => $item) { if (empty($item->source_language_code)) { $x_default = $lang; break; } } } if (isset($x_default, $hreflang_items[$x_default])) { $hreflang_items['x-default'] = $hreflang_items[$x_default]; } } return $hreflang_items; });
NOTE: Always have a recent and working backup before making any changes recommended by our team or a 3rd party.
If this solution does not resolve your issue or seems irrelevant due to being outdated or not applicable to your case, we recommend opening a new support ticket. Additionally, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum at WPML Support Forum.
This is the technical support forum for WPML - the multilingual WordPress plugin.
Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.
This topic contains 4 replies, has 2 voices.
Last updated by 2 years, 5 months ago.
Assisted by: Bobby.