Wagatail Embeds YouTube - Prevents Related Videos Showing

I need to make sure that linked videos are not showing in YouTube videos on a customer site built with Wagtail. Currently they all use the built-in wagtailembeds function via wagtailembeds_tags {% embed video.url%}.

I've usually done this before by adding a GET parameter 'rel = 0' to the url. I tried this through the url field on the page editor screen, but it seems that at some point in its processing it gets disabled.

I currently don't see any way to do this? From looking at the most recent project branch in ReadTheDocs it seems like there might be a way to set up the oEmbed provider soon, just I need a solution now.

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

Thanks in advance for your help!

+3


source to share


1 answer


I solved this by implementing my own template tag. It might be a quick and somewhat messy solution (the usual vendor might be the official way), but it works and is sufficient for me.

The user pastes a regular URL. If the url is for Youtube or Vimeo, custom template tags will take care of that. 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'),
    ]

      

+1


source







All Articles