Show a placeholder with a "Load video" button instead of YouTube and Vimeo iframes, so nothing loads from them until the visitor agrees.
add_filter( 'embed_oembed_html', function ( string $html, string $url ): string {
$host = wp_parse_url( $url, PHP_URL_HOST ) ?: '';
if ( ! preg_match( '/(^|\.)(youtube\.com|youtu\.be|vimeo\.com)$/i', $host ) || ! str_contains( $html, '<iframe' ) ) {
return $html;
}
$service = str_contains( $host, 'vimeo' ) ? 'Vimeo' : 'YouTube';
return sprintf(
'<div class="nahnu-embed"><template>%1$s</template><button type="button" class="nahnu-embed__button">Load video from %2$s</button><p class="nahnu-embed__note">The video will load from %2$s, which may set cookies.</p></div>',
$html,
esc_html( $service )
);
}, 20, 2 );
add_action( 'wp_footer', function (): void {
?>
<style>
.nahnu-embed{aspect-ratio:16/9;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:.5rem;background:#111;color:#fff;border-radius:8px;padding:1rem;text-align:center}
.nahnu-embed__button{font:inherit;font-weight:600;padding:.7rem 1.2rem;border:0;border-radius:6px;background:#fff;color:#111;cursor:pointer}
.nahnu-embed__note{margin:0;font-size:.85em;opacity:.8}
</style>
<script>
document.addEventListener( 'click', function ( event ) {
var button = event.target.closest( '.nahnu-embed__button' );
if ( ! button ) {
return;
}
var box = button.closest( '.nahnu-embed' );
var frame = box.querySelector( 'template' ).content.cloneNode( true );
box.replaceWith( frame );
} );
</script>
<?php
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Video iframes from YouTube and Vimeo contact those services as soon as the page loads, sharing the visitor’s IP address and often setting cookies. Under GDPR many sites need consent first. This snippet only loads the video after the visitor clicks.
embed_oembed_html receives the embed HTML WordPress built. For YouTube and Vimeo links, the snippet puts the real iframe inside a <template> tag, which browsers don’t load, and shows a button and a short note instead.
A small script in the footer listens for clicks on those buttons and replaces the placeholder with the iframe from the template. A few lines of CSS give the placeholder a 16:9 shape so the page doesn’t jump.
View a post with a YouTube embed. You should see a dark box with “Load video from YouTube”. The network panel should show no requests to YouTube until you click.