Skip to content

Instantly share code, notes, and snippets.

@audrasjb
Created January 29, 2025 13:19
Show Gist options
  • Save audrasjb/fa4d7462206bd4829ef6b2a4e365c397 to your computer and use it in GitHub Desktop.
Save audrasjb/fa4d7462206bd4829ef6b2a4e365c397 to your computer and use it in GitHub Desktop.
Force no-cookie domain for Youtube embeds
<?php
/*
Plugin Name: Force no-cookie domain for Youtube embeds.
Plugin URI: https://whodunit.fr
Description: Forces no-cookie domain on every Youtube embed.
Author: Whodunit
Version: 1.0
Author URI: https://whodunit.fr
*/
/**
* Filters embed URLs to force replace domain with no-cookie.
*
* @param string|false $cache The cached HTML result, stored in post meta.
*/
function who_force_youtube_nocookie_domain( $cache ) {
// Use youtube-nocookie.com for YouTube embeds.
if ( str_contains( $cache, 'https://www.youtube.com/embed' ) ) {
$from = 'src="https://www.youtube.com/embed/';
$to = 'src="https://www.youtube-nocookie.com/embed/';
$cache = str_replace( $from, $to, $cache );
}
return $cache;
}
add_filter( 'embed_oembed_html', 'who_force_youtube_nocookie_domain' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment