Remove hreflang tags from {{ content_for_header }}

Hi there, I want to be able to generate the hreflang tags myself and not rely on the one generated by Shopify.

The main reason is that some pages only exist in English, even on the non English version of my website, and I want those non translated pages to point to their canonical English version.

Generating the proper hreflang tags is relatively straightforward, however shopify automatically creates hreflang tags in the {{ content_for_header }} liquid object that will conflict with the one I generate.

Since {{ content_for_header }} can’t be edited, is there another way to prevent Shopify adding the tags in {{ content_for_header }}?

3 Likes

Are you finding that the hreflang tags are still being auto added, even after you add your custom ones?

Yes, the hreflang tags are still being auto added, even after I add the custom ones:

So basically I will have for instance the English version of a random collection with:

The French version (but not translated) of the random collection (with URL"https://domain.com/fr/collections/random-collection") also with:

My problem is that because the French version of the collection exists Shopify automatically adds:

in the URL “https://domain.com/collections/random-collection”

I want to remove:

This can’t be an alternate since it is not a canonical page. Since the collection is not translated there are actually no alternate to this page.

The easy solution would be to edit {{ content_for_header }} to avoid that but it doesn’t seem possible

2 Likes

Ok, so I’ve been able to solve my problem.

The solution: contact Shopify staff and ask them to remove the Hreflang Tags from {{ content_for_header }} on their side. It took them a few days to do it.

4 Likes

is it still the case that Shopify staff has to do it? and if so, how to contact them pls? thx

Hi @Popkeeper ,

you can remove them via replace as I’ve described here:

https://community.shopify.com/c/technical-q-a/how-to-remove-hreflang-multilingual-tag-only-keep-en-and-nl/m-p/2526278#M155650

Hi @Popkeeper

Have you already found a solution? I had the same problem. Maybe it will help other community members.

The solution works for me, I have tested several stores.

{% capture content_for_header_custom %}
{{ content_for_header }}
{% endcapture %}

{% assign content_for_header_filtered = ‘’ %}
{% assign lines = content_for_header_custom | split: ‘\n’ %}

{% for line in lines %}
{% unless line contains ‘hreflang=’ %}
{% assign content_for_header_filtered = content_for_header_filtered | append: line | append: ‘\n’ %}
{% endunless %}
{% endfor %}

{{ content_for_header_filtered }}

Only the lines with “hreflang” are removed here.
Then you can set your own “hreflangs” afterwards.

Best regards

To remove specific lines depending on the hreflang what worked for me was:

{% capture content_for_header_filtered %}
{{ content_for_header }}
{% endcapture %}

{% assign languages = ‘fo-FR|af-CA’ | split: ‘|’ %}
{% for language in languages %}
{% assign language_parts = language | split: ‘-’ %}
{% assign country_code = language_parts[1] | downcase %}
{% assign language_code = language_parts[0] | downcase %}
{% if template contains “index” %}
{% assign link_to_remove = ‘’ %}
{% else %}
{% assign link_to_remove = ‘’ %}
{% endif %}
{% assign content_for_header_filtered = content_for_header_filtered | replace: link_to_remove, ‘’ %}
{% endfor %}
{{ content_for_header_filtered }}

1 Like

This works I just called them and they were able to remove it.