Wagatail embeds YouTube - prevent related videos showing

Viewed 619

I need to ensure that related videos dont show on youtube videos in a clients site built with Wagtail. They are all presently using the built in wagtailembeds feature via wagtailembeds_tags {% embed video.url %}.

Typically I have done this before by appending the GET parameter 'rel=0' to the URL. I have tried this via the URL field in the page editor screen but it seems it gets stripped off at some stage of its processing.

At present I can't see any way of doing this? From looking at the latest branch of the project in ReadTheDocs it seems there may be a way of customising an oEmbed provider soon, just I need a solution now.

http://docs.wagtail.io/en/latest/advanced_topics/embeds.html

Thanks in advance for any help!

1 Answers

I solved this by implementing a custom template tag. It is perhaps a quick and somewhat dirty solution (a custom provider might be the official way to go), but it works and that's enough for me.

The user inserts a regular URL. If the URL is for Youtube or Vimeo, the custom template tags takes care of it. Otherwise, the template uses the default Wagtail providers.

project/templatetags/custom_template_tags.py:

import re
from django import template

register = template.Library()

@register.filter
def get_embed_url_with_parameters(url):
    if 'youtube.com' in url or 'youtu.be' in url:
        regex = r"(?:https:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)"  # Get video id from URL
        embed_url = re.sub(regex, r"https://www.youtube.com/embed/\1", url)                 # Append video id to desired URL
        embed_url_with_parameters = embed_url + '?rel=0'                                    # Add additional parameters
        return embed_url_with_parameters
    elif 'vimeo.com' in url:
        embed_url = url.replace('vimeo.com', 'player.vimeo.com/video')
        embed_url_with_parameters = embed_url + '?loop=0&title=0&byline=0&portrait=0'
        return embed_url_with_parameters
    else:
        return None

/project/templates/video_embed.htm:

{% load wagtailcore_tags %}
{% load wagtailembeds_tags %}
{% load custom_template_filters %}


{% with value.embed.url as regular_url %}
    {% with regular_url|get_embed_url_with_parameters as embed_url %}

        <div class="container">
            <div class="block-description cvast-embed cvast-spacer-top">
                <div align="center">
                    <h5>{{ value.title }}</h5>
                    {% if embed_url is None %}
                        {% embed regular_url %}
                    {% else %}
                        <iframe src="{{ embed_url }}" frameborder="0" allowfullscreen></iframe>
                    {% endif %}
                </div>
            </div>
        </div>

    {% endwith %}
{% endwith %}

project/models.py:

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.blocks import StructBlock
from wagtail.wagtailembeds.blocks import EmbedBlock
from wagtail.wagtailadmin.edit_handlers import FieldPanel

class EmbedVideoBlock(StructBlock):
    embed = EmbedBlock()

    class Meta:
        template = "blocks/embed_video_block.htm"

class YourPage(Page):
    video = EmbedVideoBlock()
    content_panels = Page.content_panels + [
        FieldPanel('video'),
    ]
Related