Halloween Sale 15% off Ends in See the plans
PHP

Load Social Media Post Embeds Only on Click

Holds back embedded posts from X (Twitter), Instagram, Facebook, TikTok and Threads behind a short notice, so their scripts and trackers only load when the visitor asks for them.

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

Code

// Provider URL fragment => name shown to visitors.
$wpide_social_embeds = array(
    'twitter.com'   => 'X (Twitter)',
    'x.com'         => 'X (Twitter)',
    'instagram.com' => 'Instagram',
    'facebook.com'  => 'Facebook',
    'tiktok.com'    => 'TikTok',
    'threads.net'   => 'Threads',
);

add_filter( 'embed_oembed_html', function ( $html, $url ) use ( $wpide_social_embeds ) {
    $host = (string) wp_parse_url( $url, PHP_URL_HOST );
    $name = '';

    foreach ( $wpide_social_embeds as $domain => $label ) {
        if ( $host === $domain || substr( $host, -strlen( '.' . $domain ) ) === '.' . $domain ) {
            $name = $label;
            break;
        }
    }

    if ( ! $name || false === stripos( $html, '<script' ) ) {
        return $html;
    }

    $preview = wp_trim_words( wp_strip_all_tags( preg_replace( '#<script\b.*?</script>#is', '', $html ) ), 40 );

    return sprintf(
        '<div class="wpide-social-embed"><template>%1$s</template><div style="padding:16px;border:1px solid #c3c4c7;border-radius:8px;"><p style="margin:0 0 8px">%2$s</p><p style="margin:0 0 8px;font-size:.9em">%3$s <a href="%4$s" target="_blank" rel="noopener">View on %5$s</a></p><button type="button" class="wpide-social-load">%6$s</button></div></div>',
        $html,
        esc_html( $preview ),
        esc_html( sprintf( 'Loading this post sends data to %s.', $name ) ),
        esc_url( $url ),
        esc_html( $name ),
        esc_html( 'Show post from ' . $name )
    );
}, 20, 2 );

add_action( 'wp_footer', function () {
    ?>
<script>
document.addEventListener('click', function (e) {
    var btn = e.target.closest ? e.target.closest('.wpide-social-load') : null;
    if (!btn) { return; }
    var box = btn.closest('.wpide-social-embed');
    var tpl = box.querySelector('template');
    var frag = tpl.content.cloneNode(true);
    frag.querySelectorAll('script').forEach(function (old) {
        var s = document.createElement('script');
        Array.prototype.forEach.call(old.attributes, function (a) { s.setAttribute(a.name, a.value); });
        s.text = old.text;
        old.replaceWith(s);
    });
    box.replaceChildren(frag);
});
</script>
    <?php
} );

Notes

The text of the post is still shown inside the notice when the provider includes it in the embed code.

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.