Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are trying to configure the hreflang tags for your multilingual site and need the x-default hreflang tag to be set on the English version instead of the Italian version, which is the default language.
Solution:
We recommend using the wpml_hreflangs filter to adjust the hreflang tags. Here is a proof of concept code:

add_filter( 'wpml_hreflangs', function( $hreflang_items ) {
    if ( is_array( $hreflang_items ) ) {
        foreach( $hreflang_items as $hreflang_code => $hreflang_url ) {
            if( $hreflang_code == 'x-default' ) {
                $hreflang_items['x-default'] = $hreflang_items['en'];
            }
        }
    }
    return $hreflang_items;
} );

Add this code to the functions.php file of a staging copy of your site and adapt it as needed. If you are not comfortable implementing this, consider contacting one of our certified partners.
For further examples and discussions related to hreflang tags, you can visit these threads:

If this solution does not apply to your case, or if it seems outdated, 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. If further assistance is needed, please open a new support ticket.

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.

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nicolas V. 2 months, 3 weeks ago.

Assisted by: Nicolas V..

Author Posts
July 2, 2024 at 11:03 am #15874524

raffaeleV-5

Background of the issue:
I am trying to configure the hreflang tags for my site hidden link. My site is hidden link (Italian version) and I don't want to use the "/it" folder. The English version is hidden link. At the moment, WPML inserts "x-default" on the Italian version, but I should set it on the English version. The code I should insert is the following:

Symptoms:
WPML inserts "x-default" on the Italian version instead of the English version.

Questions:
How can I configure WPML to set the x-default hreflang tag on the English version instead of the Italian version (default language)?

July 2, 2024 at 12:48 pm #15875322

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hi again Raffael,

First I'd like to mention that it's a normal behavior. WPML will always declare the default language as the x-default language. In order to modify this behavior you can use one of our hooks 'wpml_reflangs'.

IMPORTANT: Please note that I'm not a developer and helping you with custom code, is out of the scope of WPML. But I'd like to point you in the right direction, so I'm providing you with a "proof of concept" code.

If you are not able to accomplish this, I would recommend you contact one of our certified partners who will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

add_filter( 'wpml_hreflangs', function( $hreflang_items ) {
    if ( is_array( $hreflang_items ) ) {
        foreach( $hreflang_items as $hreflang_code => $hreflang_url ) {
            if( $hreflang_code == 'x-default' ) {
                $hreflang_items['x-default'] = $hreflang_items['en'];
            }
        }
    }
    return $hreflang_items;
} );

Add this code to the functions.php file of a staging copy of your site and try to adapt it to fit your needs.

Here are some examples:
- https://wpml.org/forums/topic/multiple-hreflang-tags-for-one-language-and-one-url/#post-14674975
- https://wpml.org/forums/topic/x-default-tag-in-our-page-how-to-skip-or-deactivate/#post-10437001
- https://wpml.org/forums/topic/hreflang-x-default-is-missing-from-some-pages-2/#post-11625629

I hope it will help you.
Thanks

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