Privacy PHP Intermediate

Load Video Embeds Only When Clicked

Show a placeholder with a "Load video" button instead of YouTube and Vimeo iframes, so nothing loads from them until the visitor agrees.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 35 lines
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
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?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.

How it works

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.

Things to watch

  • Visitors need to click twice to play: once to load, once to play. That’s the trade-off for privacy.
  • If your cookie banner already blocks embeds until consent, you don’t need this.
  • Existing embeds change once WordPress refreshes its embed cache, or when you re-save the post.

How to test it

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.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only