Last active
August 8, 2023 16:59
-
-
Save razaanstha/af10490a09422231094e1cb4ea5a2b2c to your computer and use it in GitHub Desktop.
Enhancing YouTube and Vimeo Video Embeds for GDPR Compliance in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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