Last active
October 14, 2021 17:24
-
-
Save walterebert/883359880ee30cc8b5e68163ed295ede to your computer and use it in GitHub Desktop.
WordPress: Don't use cookies with YouTube
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
<?php | |
/* | |
Use YouTube privacy-enhanced mode. More info: https://support.google.com/youtube/answer/171780?visit_id=637419781721286051-946666784&rd=1 | |
*/ | |
add_filter( | |
'embed_oembed_html', | |
function( $cache, $url, $attr, $post_ID ) { | |
if ( preg_match('#^https?://(www\.)?youtu\.?be(\.com)?/#i', $url) ) { | |
return str_replace( | |
' src="https://www.youtube.com/embed/', | |
' loading="lazy" src="https://www.youtube-nocookie.com/embed/', | |
$cache | |
); | |
} | |
return $cache; | |
}, | |
11, | |
4 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment