Skip to content

Instantly share code, notes, and snippets.

@blueprintmrk
Forked from kitop/twitter-cards.liquid
Created June 9, 2020 15:40
Show Gist options
  • Save blueprintmrk/920ff575a34e3d51e54c227554db5e4b to your computer and use it in GitHub Desktop.
Save blueprintmrk/920ff575a34e3d51e54c227554db5e4b to your computer and use it in GitHub Desktop.
Twitter Cards Snippet for Shopify Stores
{% comment %}
This is for widgets that share content on Twitter.
Twitter developer info: https://dev.twitter.com/docs/cards
Brought to you by Viralica http://viralica.com
{% endcomment %}
{% if template contains 'product' %}
<meta name="twitter:card" content="product">
<meta name="twitter:title" content="{{ product.title }}" />
<meta name="twitter:description" content="{{ product.description | strip_html | strip_newlines | truncatewords: 160, '' | escape }}" />
<meta name="twitter:image" content="{{ product.featured_image | product_img_url: 'medium' }}" />
<meta name="twitter:data1" content="{{ product.price | money_with_currency }}">
<meta name="twitter:label1" content="PRICE">
{% if product.available %}
<meta name="twitter:data2" content="In stock!">
{% else %}
<meta name="twitter:data2" content="Out of stock">
{% endif %}
<meta name="twitter:label2" content="AVAILABILITY">
{% elsif template contains 'article' %}
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="{{ article.title }}" />
<meta name="twitter:description" content="{{ article.excerpt_or_content | strip_html | truncatewords: 160, '' | escape }}" />
{% if article.content contains "<img" %}
{% assign src = article.content | split: 'src="' %}
{% assign src = src[1] | split: '"' | first | replace: '//cdn', 'http://cdn' | replace: 'http:http://', 'http://' | remove: 'https:' %}
{% if src %}<meta property="twitter:image" content="{{ src }}" />{% endif %}
{% endif %}
{% else %}
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="{{ shop.name | truncatewords: 70, '' }}" />
<meta name="twitter:description" content="{{ page_description | strip_html | truncatewords: 160, '' | escape }}" />
{% if settings.logo_image == 'logo.png' %}
<meta name="twitter:image" content="{{ 'logo.png' | asset_url }}" />
{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment