Skip to content

Instantly share code, notes, and snippets.

@razaanstha
Last active August 8, 2023 16:59
Show Gist options
  • Save razaanstha/af10490a09422231094e1cb4ea5a2b2c to your computer and use it in GitHub Desktop.
Save razaanstha/af10490a09422231094e1cb4ea5a2b2c to your computer and use it in GitHub Desktop.
Enhancing YouTube and Vimeo Video Embeds for GDPR Compliance in WordPress
add_filter('embed_oembed_html', 'gdpr_compliant_embed_for_youtube_and_vimeo');
function gdpr_compliant_embed_for_youtube_and_vimeo($html)
{
if (preg_match('/src="(.+?)"/', $html, $matches)) {
$source = !empty($matches[1]) ? parse_url($matches[1]) : '';
$host = !empty($source['host']) ? $source['host'] : '';
// For Youtube embedded videos
if ($host && ($host == 'www.youtube.com' || $host == 'youtu.be')) {
$html = str_replace($host, 'www.youtube-nocookie.com', $html);
}
// For Vimeo embedded videos
if ($host && $host == 'player.vimeo.com' && strpos($matches[1], 'dnt=1') === false) {
$html = str_replace($matches[1], $matches[1] . '&dnt=1', $html);
}
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment