Halloween Sale 15% off Ends in See the plans
PHP

Load Vimeo Videos Only After a Click

Replaces embedded Vimeo players with a lightweight placeholder showing the video title, so nothing is loaded from Vimeo until the visitor clicks to play.

Don’t have WPIDE yet? Install it on your site

Code

add_filter( 'embed_oembed_html', function ( $html, $url ) {
    if ( false === stripos( $url, 'vimeo.com' ) || ! preg_match( '#<iframe[^>]+src="([^"]*player\.vimeo\.com[^"]*)"#i', $html, $src ) ) {
        return $html;
    }

    $title = preg_match( '#title="([^"]*)"#i', $html, $t ) ? html_entity_decode( $t[1], ENT_QUOTES ) : 'Vimeo video';
    $player = add_query_arg( array( 'autoplay' => 1, 'dnt' => 1 ), html_entity_decode( $src[1] ) );

    return sprintf(
        '<div class="wpide-vimeo" data-src="%1$s" style="position:relative;aspect-ratio:16/9;background:#1a2e3b;color:#fff;display:flex;align-items:center;justify-content:center;flex-direction:column;gap:10px;padding:16px;text-align:center;"><p style="margin:0;font-weight:600">%2$s</p><button type="button" class="wpide-vimeo-play" style="padding:10px 18px;border:0;border-radius:6px;background:#00adef;color:#fff;cursor:pointer;">Play video from Vimeo</button><small>Vimeo will receive your IP address when the video loads.</small></div>',
        esc_url( $player ),
        esc_html( $title )
    );
}, 20, 2 );

add_action( 'wp_footer', function () {
    ?>
<script>
document.addEventListener('click', function (e) {
    var btn = e.target.closest ? e.target.closest('.wpide-vimeo-play') : null;
    if (!btn) { return; }
    var box = btn.closest('.wpide-vimeo');
    var frame = document.createElement('iframe');
    frame.src = box.getAttribute('data-src');
    frame.title = box.querySelector('p').textContent;
    frame.allow = 'autoplay; fullscreen; picture-in-picture';
    frame.allowFullscreen = true;
    frame.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;border:0;';
    box.replaceChildren(frame);
    frame.focus();
});
</script>
    <?php
} );

Notes

The placeholder uses no image from Vimeo on purpose, so no request reaches Vimeo before the click.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.