1

I'd like to avoid loading as much foreign Javascript and Cookies as possible on a couple sites, and I'd like to know if there's an easy way to avoid the embedding of YouTube videos, and instead, load a thumbnail, and on click, open them in a new window/tab.

Everything I see around oEmbed is about embedding the videos, not de-embedding them, :P

There are a bunch of "load defer" or "lazy load" plugins, and the nearest I got to it has been with "WP YouTube Lyte": it gets the video thumbnail from youtube, adds a "play" icon, and serves it statically on the page. Once you click on it, it loads the Youtube player.

This would be great if, instead of loading the player in place, it would send you to a new page, avoiding any cross-loading of scripts and cookies.

Can I use oEmbed in my favor?

2 Answers 2

1

Simple Shortcode:

function alternative_youtube_embed_func( $atts ) {
    $a = shortcode_atts( array(
        'id' => 'ScMzIvxBSi4' // youtube video ID from video URL
    ), $atts);
    return '<a href="https://www.youtube.com/watch?v=' . $a["id"] . '" rel="noopener noreferrer" target="_blank"><img width="560" height="315" src="https://img.youtube.com/vi/' . $a["id"] . '/0.jpg" alt="link to youtube video" /></a>';
}
add_shortcode( 'alternative-youtube-embed', 'alternative_youtube_embed_func' );

Add a play-button overlay with CSS, and voilà.

Plugin with more complex shortcode options: This plugin blocks loading youtube videos before consent for GDPR compliance and has the feature you want as one of the shortcode parameters:

https://www.altmann.de/en/blog-en/code-snippet-gdpr-compliant-youtube-videos/#new-window

Additional Info: You might want to remove the embed blocks so users can't use them, see here.

1
  • This sounds interesting, I'm going to try it...
    – Andor
    May 10, 2023 at 15:32
1

I am trying to do the exact same as you. I installed a plugin, think it's called lazy load. It made a nice, local thumbnail for my YT video in my WP post. Then I installed a plugin, that opens external links in a new window. Problem is: When you click the YT link/image in the WP post, it opens in a new window and start playing the video AND at the same time, plays the video in the WP post. This is a simple way: (maybe too simple, I miss the play-button) https://stackoverflow.com/questions/54719610/how-to-open-youtube-embedded-video-in-new-tab

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.