Aleš Sýkora / November 28, 2023 / 0 comments
Change youtube to youtube-nocookie with ACF embed field
2 min read / ACF, Custom Code, Plugins, WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: With Cookie and GDPR laws, you should display the YouTube and other embeds only after user consent to marketing cookies. There is a way to avoid this but still send user IP address to YouTube and store one cookie instead of multiple ones. The way is a change of YouTube Embed URL to “youtube-nocookie.com” .…
With Cookie and GDPR laws, you should display the YouTube and other embeds only after user consent to marketing cookies.
There is a way to avoid this but still send user IP address to YouTube and store one cookie instead of multiple ones. The way is a change of YouTube Embed URL to “youtube-nocookie.com” .
Before:
After:
Here is the way to change the source “youtube.com” to “youtube-nocookie.com” with the advanced custom field oEmbed:
When you want to display the YouTube iframe directly:
Field is not in ACF repeater
<div class="embed-container">
<?php echo str_replace('https://www.youtube.com/embed', 'https://www.youtube-nocookie.com/embed', get_field('iframe_field')); ?>
</div>
Field is within ACF repeater
<div class="embed-container">
<?php echo str_replace('https://www.youtube.com/embed', 'https://www.youtube-nocookie.com/embed', get_sub_field('iframe_field')); ?>
</div>
When you want to store the new YouTube Nocookie address in variable:
<?php
$iframe = get_field('video');
$cookieframe = str_replace('youtube.com/embed/', 'youtube-nocookie.com/embed/', $iframe);
?>
<div class="embed-container"><?php echo $cookieframe ?></div>
?>
(change get_field to get_sub_field if working in ACF repeater)
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻