Lavorare con YouTube

Se incorpori video di YouTube nel tuo sito web, devi assicurarti che questi vengano bloccati in assenza di consenso.

Nota: ti consigliamo di utilizzare i "codici no-cookie" di YouTube. Per farlo, basta sostituire www.youtube.com nel <iframe ...> codice con www.youtube-nocookie.com

Codice di esempio prima delle modifiche:

<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>

Codice di esempio dopo le modifiche:

<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>

Per i video di YouTube di dimensioni superiori a 300×300 pixel, il CMP applicherà automaticamente il blocco dei contenuti dinamici.

YouTube nei siti web WordPress

Per adattare il codice di incorporamento di YouTube in un sito web WordPress, puoi utilizzare il seguente codice WordPress (aggiungilo al file functions.php del tema del tuo sito WordPress) per formattare automaticamente tutti i video di YouTube nel tuo sito web:

// 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);

We do our best to keep this purely informative documentation up to date. However, if you notice that any of these guides need a little touch-up, let us know!