Privacy PHP Beginner

Use YouTube’s Privacy-Enhanced Mode for Embeds

Switch every YouTube embed to youtube-nocookie.com, which doesn't set tracking cookies until the visitor plays the video.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 6 lines
add_filter( 'embed_oembed_html', function ( string $html, string $url ): string {
	if ( ! str_contains( $html, 'youtube.com/embed/' ) ) {
		return $html;
	}
	return str_replace( 'youtube.com/embed/', 'youtube-nocookie.com/embed/', $html );
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Standard YouTube embeds set cookies as soon as the page loads, even if nobody presses play. YouTube’s privacy-enhanced mode, on youtube-nocookie.com, waits until the video is played. This snippet switches every embed automatically.

How it works

When WordPress turns a YouTube link into an embed, the HTML passes through embed_oembed_html. The snippet swaps the iframe’s address to the youtube-nocookie.com version. The video, player and controls are the same.

Things to watch

  • Once the visitor presses play, YouTube may still set cookies and track viewing. For full consent before loading, use “Load Video Embeds Only When Clicked”.
  • WordPress caches embed HTML. Existing embeds update the next time their cache refreshes, or when you edit and save the post.

How to test it

View a post with a YouTube embed and inspect the iframe. Its src should start with https://www.youtube-nocookie.com/embed/.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only