Info
Content

Working with YouTube

If you are embedding YouTube videos in your website, you should ensure that these are blocked if no consent is given.

Note: We recommend to use YouTube's "no-cookie codes". In order to do this, simply replace www.youtube.com in the <iframe ...> code with www.youtube-nocookie.com

Example code before changes:

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/gHTrl91Rdls" 
        frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" 
        allowfullscreen></iframe>

Example code after changes:

<iframe data-cmp-vendor="s30" src="about:blank" class="cmplazyload" 
        width="560" height="315" 
        data-cmp-src="https://www.youtube-nocookie.com/embed/gHTrl91Rdls" frameborder="0" 
        allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" 
        allowfullscreen ></iframe>

For YouTube videos that are bigger than 300x300 pixels, the CMP will automatically apply dynamic content blocking.

YouTube in Wordpress websites

In order to adjust the YouTube embed code in a wordpress website, you can use the following WordPress-Code (add it to the functions.php of your WordPress design theme) to automatically format all YouTube videos in your website:

// customize wordpress gutenberg's core youtube block
function cmp_youtube_player($block_content, $block)
{
	if ("core/embed" === $block['blockName'] && "youtube" === $block['attrs']['providerNameSlug']) {
		$block_content = str_replace('?feature=oembed', '?feature=oembed&rel=0', $block_content);
		$block_content = str_replace(' src="https://www.youtube.com/', ' src="about:blank" data-cmp-src="https://www.youtube-nocookie.com/', $block_content);
        $block_content = str_replace(' src="https://www.youtube-nocookie.com/', ' src="about:blank" data-cmp-src="https://www.youtube-nocookie.com/', $block_content);
		$block_content = str_replace('<iframe ', '<iframe data-cmp-vendor="s30" class="cmplazyload" ', $block_content);
	}
	return $block_content;
}
add_filter('render_block', 'cmp_youtube_player', 10, 2);

Back to top