Discover in this tutorial how to load the cookie-free version of YouTube using the native WordPress block, via code.
The WordPress native block for embedding YouTube videos is very practical, since just by pasting a YouTube URL, it renders the content of the video.
The catch is that if you want to use the version that allows you to serve the video without cookies (to comply with the GDPR) the YouTube embed block does not recognize the URL (youtube-nocookie.com) and does not render the video.
The ideal would be to have the facility to paste the usual YouTube URL but that the visitor is shown the version without cookies.
So, in today’s tutorial you are going to get the cookie-free version loaded using the native YouTube block thanks to the render_block_{$this->name}
filter and the WP_HTML_Tag_Processor
class.
Let’s see how:
Steps to display the cookie-free version of a YouTube video using the embed block in WordPress
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1091 code snippets of this website, login or subscribe here.
In this code you use the render_block_{$this->name}
filter in combination with the WP_HTML_Tag_Processor
class.
It detects when you are in the embedded YouTube block and replace youtube.com with youtube-nocookie.com.
However, keep in mind that since you are using a PHP filter you have the limitation that you will only see the result in the web frontend, in the backend the changes will not be reflected.
Conclusions
Now you know how to get the native YouTube embedded video block to display the cookie-free version.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉