Skip Navigation

Resolved

Reported for: WPML Multilingual CMS 3.3.6

Resolved in: 3.3.6

Overview of the issue

The hreflang meta tags may be missing from the page header. This is happening since WPML 3.3.6, in combination with themes or plugins which are loading WPML too soon.

WPML should include <link hreflang=”lang”> tags in your sites header section. This is missing in some sites using WPML 3.3.6 and in combination with some plugins or themes. The problem happens because WPML is attempting to output these tags early in the header, but other plugins are causing a race condition, triggering WPML too early. You may receive an email from Google Webmaster tool, saying “Incorrect hreflang implementation”.

The complete description of this functionality appears in Google’s content guidelines for language and regional URLs.

Workaround

To fix this in WPML 3.3.6:

  1. Open the file sitepress-multilingual-cms/sitepress.class.php
  2. Scroll down to the bottom of this file, where you must see this:
    Screen Shot 2016-02-24 at 10.47.11
  3. Replace this line:
    add_action( 'wp_head', array( $this, 'head_langs' ), 1 );
  4. With:
    add_action( 'wp_head', array( $this, 'head_langs' ) );
  5. The final version of this code must look like that:
    Screen Shot 2016-02-24 at 11.00.04
  6. Save the file

In the next version of WPML, the same fix will be applied.

The code at step #3 was meant to have the hreflang meta tags printed as early as possible, but this cause issues with some themes and plugins.

If you still want to have the hreflang meta tags printed as early as possible, you can apply a more complex solution. Add this code to your theme’s functions.php file:

function my_handle_head_hreflang() {
   global $sitepress;

   $settings = $sitepress->get_settings();
   if ( $settings[ 'seo' ][ 'head_langs' ] ) {
      remove_action( 'wp_head', array( $sitepress, 'head_langs' ) );
      add_action( 'wp_head', array( $this, 'head_langs' ), 1 );
   }
}

add_action('wpml_loaded', 'my_handle_head_hreflang');

15 Responses to “hreflang links output is wrong in some installations”

    • Dear Antonella Babini,

      did this fix solve your problem? i have the same and would like to know if the changes helped before i invest time into it.

      Thank you in advance
      Bernhard

  1. Hi there!

    Firstly thank you for sharing this fix.

    I tried to follow the solution, and I had completed from point 1 till point 6 of the fix.

    But still not woorking properly.

    I’m using Avada theme and the way WPML is creating hreflang in the source code is:

    1) Is not linking the related language version, instead links the home page in each language

    2) When i tried the fix, still show the same hreflang tag and anthing seems to change.

    This is the way WPML is generating the hreflan in the source code for the following page:
    http://www.domain.com/en/example-url

    Instead of showing the right hreflang as it should:

    Example: If I am in the english site version, in the following page (www.domain.com/en/example-url). WPMl should include in the source code in the head section the following code:

    But still showing the same:

    This fix is not working for Avada Theme 3.9.3

    I need more help!

    Regards

    • Sure 🙂

      Just FYI – Actually for Avada the authors kindly fixed on their side too, so from their next release it’ll be fixed anyway (WPML / Avada).

  2. Hello,

    Thanks for sharing this fix.

    This is my situation before and after:

    Before:

    I had the hreflang tag at line 63/63 just after title and favicon

    After:

    Now I have the hreflang tag at line 130/131 after all the stylesheets and jquery inclusions

    Do you think the solution will work better this way?

    Regards

    • Dario, this fix is intended to address very specific cases, where WPML conflicts with another plugin or with the theme. We’ve seen it happening in several places, including on our own site.

      The change that’s described here demotes the priority of the hook that displays the hreflang links. If your site displayed them correctly (for all pages and all login cases), you don’t need to apply this change.

      In any way, Google reads the ENTIRE page and will follow the hreflang links no matter where they are in the header. There is a mention in one page on Google support that you should have these links early in the header. That page doesn’t say that if the links appear later in the header, it will cause any problem.

      We checked this on our Google Webmasters account and I can confirm that once we applied the fix and the links got restored to all pages, Google sees the correct language information for the entire site. Before fixing it, Google alerted that pages have broken language links.

      I hope this helps.

  3. There was a clear statement made several times by Google that the earlier the hreflang tags are output, the better it is. Basically, you should keep the priority 1 for the output and have the theme fixed. Reverting to the old behavior will cause more troubles than anything else. You should provide a product that works and is compliant with Google guidelines rather than provide some workarounds for poorly coded themes…

    If the hreflang tags are output late in the head, risk is that Google parser won’t detect them and throw some errors in GWT. This will then affect the page ranking. I have seen this in few sites. This is because some other plugins are injecting invalid JS and HTML code in the header causing Google to believe that the head section was closed before it was really closed thus missing the hreflang tags.

    So make sure to keep priority 1 and have people to just fix their themes…

    • Jenny,

      The problem is that WPML can raise the priority of the hreflang metas, but several themes and plugins would break that, because, as explained in this errata, they try to load WPML too soon.

      We have decided to avoid this from happening, and provide a workaround to whom wants to still have these tags higher in the `header` section.
      We don’t want to ditch this issue: we just has to find a compromise to solve the many issue caused by these themes and plugins, while still providing that workaround.

      In future we may add a functionality which will allow to manually chose if these metas should be set as high as possible or leave them where they are.

  4. You’ll break the default behavior to accommodate buggy themes? This is a non sense. People should have a theme compatible with Google guidelines and WPML. I bought WPML and I expect it to be 100% compliant with Google guidelines. In this situation, the workaround is simple, if you find out that the code is loaded too early, just defer the loading later. But by default, try to load early and only defer if it fails.

    • Jenny,

      We added a feature in the upcoming WPML update (currently under QA), which will let user chose the priority of this hook.
      By default, it will be the highest priority, but users can also change it to the standard hook’s priority.

  5. I had the same issue and it cost me and my webmaster a lot of time to find this issue and (possibele?) solutions. My ranking dropped down a lot. I use the enfold theme. It is also not easy to find out which (populair) plugins are conflicting with WPML, for instance redirecting plugins like Quick Page/Post redirect

  6. Hi there,

    probably the most stupid question ever but if I goo into filezilla and go to the link sitepress-multilingual-cms/sitepress.class.php I can’t enter it. Is there a different way to hreflang links?

    Best regards,

    Jort