Friday, September 30, 2022
The Great Tit
Updated: 30. 9. 2022, Added: 30. 9. 2022

Change youtube to youtube-nocookie with ACF embed field

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)

Did I help you? Support me on Patreon!
Beep this article to your friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram