Re: How to remove hreflang multilingual tag - only keep en and nl

How to remove hreflang multilingual tag - only keep en and nl

hasansumon
Visitor
1 0 1
Hi, I'm seeking some expert advice on a Shopify-related issue. I'm encountering alternate hreflang tags.
I want to keep x-default, nl-nl, and nl-en and delete all hreflang tag but I couldn't find it on theme editor (theme.liquid file).
How can I fix this issue? Thanks in advance for your help!
 
423224435_7891055244239124_5381643663096213893_n.jpg
Replies 8 (8)

michaelivanov
Shopify Partner
56 6 12

Not sure if this is the best way to do this but you could do it with a simple replace like this:

 

Replace 

 

{{ content_for_header }}

 

inside your theme.liquid with (this is an example with hreflang tags from a different store):

 

    {% comment %}{{ content_for_header }}{% endcomment %}
    {{ content_for_header | replace: '<link rel="alternate" hreflang="en-DE" href="https://vibrantriver.myshopify.com/">
<link rel="alternate" hreflang="de-DE" href="https://vibrantriver.myshopify.com/de">
<link rel="alternate" hreflang="fr-DE" href="https://vibrantriver.myshopify.com/fr">', ''}}

 

Before:

michael_langify_0-1712786404939.png

 

After:

michael_langify_1-1712786492418.png

 

 

faruk4
Tourist
5 0 1

Hello Michaelivanov, I have a bilingual website in Turkish and English. How can I edit this code so that it is Turkish in Turkey and English in all other locations? Electirma.com TR Electrima.com/en All Location

michaelivanov
Shopify Partner
56 6 12

Hi @faruk4,

If you're using Langify then you can enable the Language Detection feature (it detects the visitors' browser language which is usually a better choice) in 

 

Langify -> Dashboard -> Switcher Configurator -> Detection & redirection

 

language detection (1).png

 

Once done every visitor with their browser language set to Turkish will see your store in Turkish.

Any visitors with other languages e.g. French as their browser language would will see your store in the fallback language which is English by default.

 

 

DIGIDY
Shopify Partner
13 0 3

hi @hasansumon 

 

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

Mox
Shopify Partner
3 0 1

Hi! Is this still working for you? I get only blank with the above solution 😞

prichter
Shopify Partner
5 0 4

Hi @Mox ,

as stupid as it may look, replacing \n with actual linebreaks will work (don't indent the lines):

 

{% assign lines = content_for_header_custom | split: "
" %}

 

{%- assign content_for_header_filtered = content_for_header_filtered | append: line | append: "
" -%}

 

You will have to 

{% assign content_for_header = content_for_header_filtered %}

at the end and then output {{ content_for_header }}, as the Shopify code editor will bitch otherwise.

 

Gotta hate Shopify / Liquid for forcing these ugly ugly hacks and workarounds on us sometimes.

 

Greetings,

Philip

prichter
Shopify Partner
5 0 4

Hi @hasansumon ,

adjusting the code from @DIGIDY :

 

 

{% liquid
  assign content_for_header_custom = content_for_header
  assign content_for_header_filtered = ''
  # language-MARKET
  assign allowed_codes = "x-def,nl-NL,nl-EN"
%}

{% assign lines = content_for_header_custom | split: "
" %}


{% for line in lines %}
  {% liquid
    assign possible_code = line | slice: 32,5
    assign use_line = false
    
    if line contains 'hreflang'
      if allowed_codes contains possible_code
        assign use_line = true
      endif
    else
      assign use_line = true
    endif
  %}
  
  {%- if use_line -%}
      {%- assign content_for_header_filtered = content_for_header_filtered | append: line | append: "
" -%}
  {%- endif -%}
  
{% endfor %}
{% assign content_for_header = content_for_header_filtered %}

 

 

This will remove all hreflang lines except the ones specified and there is no need to code your own hreflangs back in.

 

Greetings,

Philip

Proper-Job
Shopify Partner
3 0 0

Thank you for this code. I changed it a little bit to only remove hreflang="x-default" because that's my use case.

But the concept works. Do pay attention to use actual linebreaks, not \n.