YouTube videos embedded on WordPress sites 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.
Understanding YouTube’s Privacy-Enhanced Mode
YouTube offers a privacy-enhanced mode that uses the youtube-nocookie.com
domain instead of youtube.com
. This mode prevents YouTube from storing information about visitors to your site unless they interact with the video.
Here’s how the URLs differ:
- Standard:
https://www.youtube.com/embed/VIDEO_ID
- Privacy-enhanced:
https://www.youtube-nocookie.com/embed/VIDEO_ID
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.
Modifying 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
.
Modifying 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.
Important Note:
The snippets used in this article require PHP 8.0+ since they use the modern str_contains()
function. They also assume that videos are added using core WordPress functions rather than custom theme or plugin functions. If the snippet does not work, try switching themes and disabling plugins to locate the conflict.
WordPress Core Functionality
Currently, WordPress does not provide a built-in option to use privacy-enhanced YouTube embeds. When adding videos using the Gutenberg Video block or oEmbeds, WordPress generates the embed code using the standard youtube.com
URL.
Plugin Option
If you prefer not to add custom code to your site, you can use a plugin like “WP YouTube Nocookie” (available on GitHub) to achieve the same result.
By implementing these methods, you can ensure that embedded YouTube videos on your WordPress site respect user privacy and comply with cookie regulations. However, keep in mind that using the privacy-enhanced mode may affect video analytics and personalized ad serving if you’re a YouTube content creator.