Skip Navigation

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.

Our next available supporter will start replying to tickets in about 3.40 hours from now. Thank you for your understanding.

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Bobby 2 years, 2 months ago.

Assisted by: Bobby.

Author Posts
July 5, 2022 at 12:20 pm #11605495

kristinaK-10

Hallo. in some pages there is no x-default hreflang tag.

Here some examples

hidden link

hidden link

hidden link

the Default lang is the English uk.

July 5, 2022 at 6:47 pm #11608445

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-07:00)

Hi there,

it looks like your site's default language is EN-GB, at the moment the translations I provided above are all missing the connection to an EN-GB post.

It is recommended to translate from the site's default language into the translations.

You can confirm your default language for the site from WPML->Languages

July 6, 2022 at 10:12 am #11612567

kristinaK-10

The thing is that some posts are existing only in other languages and there will be no content in the default language

July 6, 2022 at 8:07 pm #11617355

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-07:00)

I understand, however, the way this works is by using the default language that is set in WPML->Languages.

If a post is missing the default language the x default will be missing as well.

The expected procedure is to create the post/pages in the default language and translate from there.

You could try the custom solution provided here to add it manually, but i believe it would still reference to the default language.
https://wpml.org/forums/topic/x-default-an-other-issue/#post-1556554

July 7, 2022 at 4:49 pm #11625629

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-07:00)

Spoke with our team regarding this as we are aware of this behavior, there is a workaround you could try to use that would allow you to use a different x default URL other than the default language.

You can add this snippet to your 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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.