Videos embedded on WordPress site, by default, use the standard youtube.com URL which makes use of cookies. Because of various cookie laws including GDPR regulations it’s important for your website visitors to approve of any cookies when visiting your site. If you have a website that is displaying YouTube videos you may be violating certain rules.
It’s always a good idea to include a cookie acceptance window on your website that visitors can use to accept or refuse cookies. But you can also remove cookies from your embedded YouTube videos.
If you don’t feel like reading the full guide you can skip to the code snippet
How to Embed YouTube Videos Without Cookies
By default when you embed a Youtube video the URL for the iFrame would look something 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 we changed youtube
to youtube-nocookie
. By using the youtube-nocookie domain you are enabling 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. So, if you are embedding your own videos and you are part of the YouTube partner program you may want to keep the cookies. Personalizing the video experience may increase your video ad click rate and watch time. Just a heads up before you remove them!
Make WordPress use the Youtube-nocookie.com Domain
Now that we know how to embed YouTube videos without cookies the next step is to modify how WordPress works. Adding videos in WordPress is usually done by linking to the video directly using the Video block in Gutenberg or oEmbeds. Here is a sample screenshot of the Gutenberg Video block in use:
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.
If you are using our Total theme and the Video element you can easily disable cookies via the “Disable Cookies” setting like such:
Hopefully in the future WordPress will update their video block to offer a similar setting so that you don’t need to use custom code to disable cookies.
Modify WordPress YouTube Embeds
There are several ways to modify your WordPress YouTube video embeds to prevent cookies. The best method is probably hooking into “embed_oembed_html
“. This hook filters the final output of any oEmbed on the site. By using this hook we can ensure that all YouTube videos are modified regardless of where they are embeded.
Here is a sample snippet you can use on your site:
/**
* Modify YouTube Embeds to Disable Cookies.
*
* @link https://www.wpexplorer.com/wordpress-youtube-embeds-nocookie/
*/
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 );
Important: The snippets used in our article require PHP 8.0+ to since we are using the modern str_contains() function. It also assumes that videos are added using core WordPress functions rather than custom theme or plugin functions. If the snippet is does not work, try switching themes and disabling plugins to locate the conflict.
Modify the Video Block
The previous snippet works well for standard oEmbed output
Modify Inline iFrames
If you have a site where you were not using the default WordPress oEmbed functionality but rather inserting the full YouTube embed code you’ll need another snippet. The following can be used as a catch-all to replace youtube.com to youtube-nocookie.com in your post/page content.
/**
* 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 );
This snippet will search for youtube.com/embed
and replace with youtube-nocookie.com/embed
inside your post content on the frontend. This way if you have any video embeds added manually they will also be updated.
You may have noticed that in the snippet I am also using a high priority of “100”. The reason for this is so that the code runs after any shortcodes are parsed to ensure videos added via custom shortcodes also have the cookies disabled.
Download the “WP YouTube Nocookie” Plugin
If you prefer to use a plugin instead of adding custom code to your site I’ve created one that you can download from Github. If you have any issues or questions let me know in the comments below!