Integrate YouTube into Wordpress without cookies in compliance with GDPR

Embed YouTube videos in Wordpress without cookies

Although I produce very little for YouTube myself, I find other people's videos very helpful. Even with Mac or iPhone problems, a video is sometimes more descriptive than writing a lot of words. Incidentally, that also shows that macOS Catalina book by Anton Ochsenkühn, which relies on many explanatory videos and thus makes macOS functions much clearer than many books that only consist of a text mess with isolated screenshots.

If you now embed Youtube videos in your Wordpress blog by just copying the video URL into the text, Wordpress replaces it with an iframe embed code on output, which unfortunately means that cookies from Youtube are blocked in the browser of the user can be set. However, given the current EU cookie situation, this is legally critical and should be avoided.

Embed YouTube videos in Wordpress without cookies
In this post I will show you how to embed YouTube videos in Wordpress without cookies. This also works for free with a change in a Wordpress file.

Difference between cookies and tracking

It is important to know that turning off cookies does not mean that YouTube can collect data about the user. Even if you use the nocookie variant of the embedding code from YouTube, by including the video in your own blog it is still possible for YouTube to collect information about the article the moment the user calls up the article. The safe way to prevent this is the Borlabs Cookie Plugin, which I will mention shortly. This requires user approval before the embed code is run.

Note: I am not a lawyer and this is also not legal advice. ;-)

Alternative 1: Borlabs cookie plugin

I chose the paid solution on my blog and I chose the Borlabs cookie plugin for WordPress installed and configured. The plug-in requires user approval before the videos can be played. At 39 euros per year, the service is okay for me, as I also earn money with my blog, but if you have little financial means and only maintain an informative blog that does not generate anything, then the second alternative may be the right choice.

In the Borlabs Cookie plug-in, cookies can be prevented with one click. After that, the user must first accept the cookies before the video can be played (screenshots: Sir Apfelot).
In the Borlabs Cookie plug-in, cookies can be prevented with one click. After that, the user must first accept the cookies before the video can be played (screenshots: Sir Apfelot).

On the other hand, those who use Google Analytics and other cookie-friendly services should perhaps consider whether investing in Borlabs Cookie is a good idea. With the plugin, many services are already preconfigured and only need to be activated. This is much easier than integrating them in a dsgvo-compliant way via detours.

Alternative 2: change Youtube embed code

Youtube has had an option for years to prevent cookies from being set when accessing the embed code for a video. Unfortunately, Wordpress does not use this option by default and instead always uses the YouTube code that uses cookies. I hope this will be changed in core WordPress soon, but for now it's up to you.

If you want to create the embed code for a video, you can activate the "Extended data protection mode" in the options.
If you create the embed code for a video, you can activate the "extended data protection mode" in the options to prevent the setting of cookies.

If you want to stop Wordpress from doing this, you have to change the funktions.php file, which can be found in every theme directory of a Wordpress installation. The path is this one:

/WORDPRESS/wp-content/themes/NAME-DES-THEMES/functions.php

You open this file (or create it if it does not yet exist) and add the following code (I found the code somewhere, but unfortunately I did not note the source):

add_filter ('embed_oembed_html', 'youtube_nocookie_loesung', 10, 4); function youtube_nocookie_solution ($ original, $ url, $ attr, $ post_ID) {$ html = str_replace ("youtube.com", "youtube-nocookie.com", $ original); $ html = str_replace ("feature = oembed", "feature = oembed & showinfo = 0", $ html); return $ html; }

If there is already code in functions.php, you can add it below. If the file is still empty, you have to “

After pasting the code, the Youtube URLs will then be replaced with the domain “youtube-nocookie.com”, which does not set cookies.

In the embed code you can see that the domain will be changed after activating the data protection mode.
In the embed code you can see that the domain will be changed after activating the data protection mode.

Important: Change your own iFrame codes manually!

Anyone who has used the YouTube embed code in the source text of the blog article here and there should change it manually and switch to the youtube-nocookie.com domain. The script mentioned above does not change their occurrence, but only intervenes if the pure URL to the YouTube video has been incorporated into the posts.

Tip: change functions.php with plugin

If tinkering in the PHP file is too dangerous for you, you can also use the plugin "Code Snippets”, which allows you to inject arbitrary code into Wordpress, as functions.php would do – except that the code is saved in the database and is not lost after a theme update. I would prefer this route as a beginner.

 

Did you like the article and did the instructions on the blog help you? Then I would be happy if you the blog via a Steady Membership would support.

Post a comment

Your e-mail address will not be published. Required fields are marked with * marked

In the Sir Apfelot Blog you will find advice, instructions and reviews on Apple products such as the iPhone, iPad, Apple Watch, AirPods, iMac, Mac Pro, Mac Mini and Mac Studio.