European Union’s General Data Protection Regulation (GDPR) takes effect on May 25, 2018 and organizations must take steps to block cookies or other personally identifiable information before user consent.
YouTube embed iframe by default loads in numerous user tracking cookies. YouTube does offer a cookie-less domain at youtube-nocookie.com
The filter below can be added to WordPress theme or plugin to modify embeds to use this cookie-free domain.
|
<?php |
|
/** |
|
* Modify YouTube oEmbeds to use youtube-nocookie.com |
|
* |
|
* @param $cached_html |
|
* @param $url |
|
* |
|
* @return string |
|
*/ |
|
function filter_youtube_embed( $cached_html, $url = null ) { |
|
|
|
// Search for youtu to return true for both youtube.com and youtu.be URLs |
|
if ( strpos( $url, 'youtu' ) ) { |
|
$cached_html = preg_replace( '/youtube\.com\/(v|embed)\//s', 'youtube-nocookie.com/$1/', $cached_html ); |
|
} |
|
|
|
return $cached_html; |
|
} |
|
add_filter( 'embed_oembed_html', 'filter_youtube_embed', 10, 2 ); |