Last active
March 2, 2023 07:39
-
-
Save chrisjhoughton/a4ba2b7e06716ef11d5b to your computer and use it in GitHub Desktop.
Twitter Card meta tags for Shopify. Add this as a snippet called "twitter-cards.liquid" in your theme, and then add {% include 'twitter-cards' %} to your theme.liquid file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% comment %} | |
This snippet renders meta data needed to create a Twitter card | |
for products and articles. | |
IMPORTANT: change @your_twitter_username with your actual twitter username! | |
Your cards must be approved by Twitter to be activated | |
- https://dev.twitter.com/docs/cards/validation/validator | |
More information: | |
- https://dev.twitter.com/docs/cards/types/product-card | |
- https://dev.twitter.com/docs/cards/types/summary-card | |
{% endcomment %} | |
<meta name="twitter:site" content="@your_twitter_username" /> | |
{% if template contains 'product' %} | |
<meta name="twitter:card" content="product" /> | |
<meta name="twitter:title" content="{{ product.title | escape }}" /> | |
<meta name="twitter:description" content="{{ product.description | strip_html | strip_newlines | truncatewords: 60, '' | escape }}" /> | |
<meta name="twitter:image" content="{{ product.featured_image.src | product_img_url: 'large' }}" /> | |
<meta name="twitter:label1" content="Price" /> | |
<meta name="twitter:data1" content="{% if product.price_varies %}From {% endif %}{{ product.price | money_with_currency | strip_html }}" /> | |
{% if product.vendor == blank %} | |
<meta name="twitter:label2" content="Availability" /> | |
<meta name="twitter:data2" content="{% if product.available %}In stock{% else %}Out of stock{% endif %}" /> | |
{% else %} | |
<meta name="twitter:label2" content="Brand" /> | |
<meta name="twitter:data2" content="{{ product.vendor | escape }}" /> | |
{% endif %} | |
{% elsif template contains 'article' %} | |
<meta name="twitter:card" content="summary" /> | |
<meta name="twitter:title" content="{{ article.title | escape }}" /> | |
<meta name="twitter:description" content="{{ article.excerpt_or_content | strip_html | truncatewords: 60, '' | escape }}" /> | |
{% assign img_tag = '<' | append: 'img' %} | |
{% if article.content contains img_tag %} | |
{% assign src = article.content | split: 'src="' %} | |
{% assign src = src[1] | split: '"' | first | remove: 'https:' | remove: 'http:' %} | |
{% if src %} | |
<meta property="twitter:image" content="http:{{ src }}" /> | |
{% endif %} | |
{% endif %} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🖤🖤