Embedded YouTube videos on your WordPress site typically use the standard youtube.com
URL, which utilizes cookies. Due to privacy regulations like GDPR, it’s important to obtain user consent for cookies. Displaying YouTube videos without proper consent might violate these regulations.
It’s generally good practice to include a cookie acceptance notice on your website. However, you can also remove cookies from your embedded YouTube videos directly.
Modify WordPress YouTube Embeds
The most effective method to prevent cookies is by hooking into embed_oembed_html
. This hook filters the final output of any oEmbed on the site, ensuring that all YouTube videos are modified regardless of where they are embedded.
Step 1: Add the following code snippet to your theme’s functions.php
file or a custom plugin:
/**
* Modify YouTube Embeds to Disable Cookies.
*/
add_filter( 'embed_oembed_html', function( $html ) {
if ( str_contains( $html, 'youtube.com' ) ) {
$html = str_replace( 'youtube.com', 'youtube-nocookie.com', $html );
}
return $html;
}, 10 );
This code checks if the embed HTML contains youtube.com
and replaces it with youtube-nocookie.com
.
Modify Inline iFrames & “Catch-All”
If you’re not using the default WordPress oEmbed functionality and instead insert the full YouTube embed code directly into your posts, you’ll need another snippet. This can be used as a catch-all to replace youtube.com
with youtube-nocookie.com
in your post/page content.
Step 1: Add the following code snippet to your theme’s functions.php
file or a custom plugin:
/**
* Modify the WordPress content output to modify youtube embed urls.
*/
add_filter( 'the_content', function( $content ) {
if ( str_contains( $content, 'youtube.com/embed' ) ) {
$content = str_replace( 'youtube.com/embed', 'youtube-nocookie.com/embed', $content );
}
return $content;
}, 100 );
The previous code searches for youtube.com/embed
and replaces it with youtube-nocookie.com/embed
inside your post content on the frontend, updating any manually added video embeds.
You may have noticed that I am using a high priority of “100” for the
add_filter
function. The reason for this, is so that the code runs after any shortcodes and blocks are parsed. This way, any videos added via custom shortcodes or WordPress blocks also have the cookies disabled.
How to Embed YouTube Videos Without Cookies
By default, when you embed a YouTube video, the URL for the iFrame looks like this:
https://www.youtube.com/embed/1iI4tAoUzgc
However, YouTube offers an alternative domain you can use to serve videos without cookies:
https://www.youtube-nocookie.com/embed/1iI4tAoUzgc
Notice how the URL is basically the same except youtube
is changed to youtube-nocookie
. Using the youtube-nocookie
domain enables the “Enhanced-Privacy Mode.”
The Privacy Enhanced Mode of the YouTube embedded player prevents the use of views of embedded YouTube content from influencing the viewer’s browsing experience on YouTube. This means that the view of a video shown in the Privacy Enhanced Mode of the embedded player will not be used to personalize the YouTube browsing experience, either within your Privacy Enhanced Mode embedded player or in the viewer’s subsequent YouTube viewing experience.
If ads are served on a video shown in the Privacy Enhanced Mode of the embedded player, those ads will likewise be non-personalized. In addition, the view of a video shown in the Privacy Enhanced Mode of the embedded player will not be used to personalize advertising shown to the viewer outside of your site or app.
Source: https://support.google.com/youtube/answer/171780
When you disable YouTube video cookies, any advertisements and related videos will no longer be catered to the viewer.
Make WordPress Use the Youtube-nocookie.com Domain
Adding videos in WordPress is usually done by linking to the video directly using the Video block in Gutenberg or oEmbeds. WordPress then uses the URL to generate the Embed code (aka iFrame) for the live site. The issue is that WordPress always uses the youtube.com
URL, and there is no way to change it to youtube-nocookie.com
without custom code.
By implementing these methods, you can ensure that embedded YouTube videos on your WordPress site respect user privacy and comply with cookie regulations.