All things Shopify and commerce
I have been learning Liquid and Shopify development over the past couple weeks and I've gotten pretty comfortable with most of it.
Now I am looking to develop a way to control the Meta NOINDEX tag in the <head> using an input setting. The idea here would be to have a "Hide from search engines" checkbox metafield on each object (Product, Page, Collection, Custom Metaobjects). Then to use this data to determine on each object record whether to hide from search engines, and if so, print the Meta NOINDEX tag in the header.
Here are my challenges:
Curious what ideas other shopify developers might have?
Solved! Go to the solution
This is an accepted solution.
I've found a solution! I think this would be valuable for others, so I will explain here.
It's not the best design, but it works. Here is the overall approach:
Here is the code for the snippet. You can check if each object exists which will indicate to you what kind of page you're on. So if the `product` object is present, then you are on a product page. If the `collection` object is present, then you're on a collection page, etc. Then you need to a big IF ELSIF statement or CASE / WHEN statement to extract the data on the given object.
snippets/meta-seo-tags.liquid
{%- liquid
assign hide_from_search_engines = false
%}
<!-- START: meta-seo-tags -->
{% comment %}
<p>Current template is: {{ template }}</p>
{% endcomment %}
{%- if product %}
{% assign hide_from_search_engines = product.metafields.custom.hide_from_search_engines %}
{%- elsif collection %}
{% assign hide_from_search_engines = collection.metafields.custom.hide_from_search_engines %}
{%- elsif page %}
{% assign hide_from_search_engines = page.metafields.custom.hide_from_search_engines %}
{%- elsif article %}
{% comment %} Blog article page logic {% endcomment %}
{% assign hide_from_search_engines = article.metafields.custom.hide_from_search_engines %}
{%- elsif blog %}
{% comment %} For a whole Blog {% endcomment %}
{% assign hide_from_search_engines = blog.metafields.custom.hide_from_search_engines %}
{%- elsif metaobject %}
{% comment %} Custom MetaObject logic {% endcomment %}
{% assign hide_from_search_engines = shop.metaobjects[metaobject.system.type][metaobject.system.handle].hide_from_search_engines %}
{%- else %}
{% comment %}
Fallback for other types of pages like the homepage.
{% endcomment %}
{% endif %}
{%- if hide_from_search_engines -%}
{% comment %} We are hiding! {% endcomment %}
<meta name="robots" content="noindex, nofollow">
{%- endif -%}
<!-- END: meta-seo-tags -->
(Edit: this did not work - I just tested it + I had a few syntax errors I had to fix but still nothing)
This is totally a guess and I have no idea if I am understanding you correctly but are you able to establish this with using metafields?
So for example:
And then putting the code into your <head> right before the meta render tag like this:
{% if page.metafields.seo.hide_from_search_engines %}
<meta name="robots" content="noindex">
{% endif %}
This would be just for pages only. But then for the individual pages you would then check the box if it is hidden or not:
I honestly have not idea if it would work. It is a good question and one I have never even thought of.
So close @beauxbreaux ! ANd thanks for the suggestion. It got my brain firing and was able to get to the solution I posted here.
I think the reason yours isn't working is b/c you're not referencing the custom metafield properly with the namespace (custom) and handle (seo_hide_...).
You have: page.metafields.seo.hide_from_search_engines
Should be: page.metafields.custom.seo_hide_from_search_engines
This is an accepted solution.
I've found a solution! I think this would be valuable for others, so I will explain here.
It's not the best design, but it works. Here is the overall approach:
Here is the code for the snippet. You can check if each object exists which will indicate to you what kind of page you're on. So if the `product` object is present, then you are on a product page. If the `collection` object is present, then you're on a collection page, etc. Then you need to a big IF ELSIF statement or CASE / WHEN statement to extract the data on the given object.
snippets/meta-seo-tags.liquid
{%- liquid
assign hide_from_search_engines = false
%}
<!-- START: meta-seo-tags -->
{% comment %}
<p>Current template is: {{ template }}</p>
{% endcomment %}
{%- if product %}
{% assign hide_from_search_engines = product.metafields.custom.hide_from_search_engines %}
{%- elsif collection %}
{% assign hide_from_search_engines = collection.metafields.custom.hide_from_search_engines %}
{%- elsif page %}
{% assign hide_from_search_engines = page.metafields.custom.hide_from_search_engines %}
{%- elsif article %}
{% comment %} Blog article page logic {% endcomment %}
{% assign hide_from_search_engines = article.metafields.custom.hide_from_search_engines %}
{%- elsif blog %}
{% comment %} For a whole Blog {% endcomment %}
{% assign hide_from_search_engines = blog.metafields.custom.hide_from_search_engines %}
{%- elsif metaobject %}
{% comment %} Custom MetaObject logic {% endcomment %}
{% assign hide_from_search_engines = shop.metaobjects[metaobject.system.type][metaobject.system.handle].hide_from_search_engines %}
{%- else %}
{% comment %}
Fallback for other types of pages like the homepage.
{% endcomment %}
{% endif %}
{%- if hide_from_search_engines -%}
{% comment %} We are hiding! {% endcomment %}
<meta name="robots" content="noindex, nofollow">
{%- endif -%}
<!-- END: meta-seo-tags -->
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024