Skip to content

Instantly share code, notes, and snippets.

@junaidkbr
Created April 5, 2020 23:58
Show Gist options
  • Save junaidkbr/79aaf8633f4e995d3bfda7f94ca3d94a to your computer and use it in GitHub Desktop.
Save junaidkbr/79aaf8633f4e995d3bfda7f94ca3d94a to your computer and use it in GitHub Desktop.
Get Youtube video thumbnail from Youtube URL in Shopify Liquid
{% comment %} We assume that we have the youtube video in form of URL {% endcomment %}
{% assign video_url = 'https://www.youtube.com/watch?v=ScMzIvxBSi4' %}
{% assign thumbnail_url = '' %}
{% comment %} First of all, we get last part of the URL that's supposedly the Youtube Video ID {% endcomment %}
{% assign video_id = video_url | split: '/' | last %}
{% comment %} but we need strip any extra URL params {% endcomment %}
{% assign video_id = video_id | split: '?' | first %}
{% comment %} Finally, remove any hash string {% endcomment %}
{% assign video_id = video_id | split: '#' | first %}
{% comment %} It's always good to add additional validations {% endcomment %}
{% if video_id != blank %}
{% assign thumbnail_url = 'http://img.youtube.com/vi/' | append: video_id | append: '/maxresdefault.jpg' %}
{% endif %}
{{ thumbnail_url }}
{% comment %}
Output:
ScMzIvxBSi4
{% endcomment %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment