WPSurfer.com

How to Disable Embeds in WordPress [Mu-Plugin]

Published on September 24, 2022 | Updated on July 8, 2024

In WordPress, embeds refer to the ability to include content from other websites in your WordPress posts and pages.

This feature is powered by a technology called oEmbed, which allows WordPress to automatically retrieve and display content from other sites in a formatted and visually appealing way.

For example, you can use embeds to include a YouTube video or a Twitter post in your WordPress content by simply pasting the URL of the video or tweet into the editor.

Embeds are a convenient way to add multimedia content to your WordPress, however, these add also add extra request, which can affect in some way the site performance.


PHP Snippet to Disable Embeds in WordPress

If you want to disable embeds on your WordPress site, you can use the following code:

<?php
/*
  Plugin Name: Embed Disabler
  Description: It disable embeds
  Version: 1.0
  Author: TicoLibre
*/
function disable_embeds_code_init() {
 // Remove the REST API endpoint.
 remove_action( 'rest_api_init', 'wp_oembed_register_route' );
 // Turn off oEmbed auto discovery.
 add_filter( 'embed_oembed_discover', '__return_false' );
 // Don't filter oEmbed results.
 remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
 // Remove oEmbed discovery links.
 remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
 // Remove oEmbed-specific JavaScript from the front-end and back-end.
 remove_action( 'wp_head', 'wp_oembed_add_host_js' );
 add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
 // Remove all embeds rewrite rules.
 add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
 // Remove filter of the oEmbed result before any HTTP requests are made.
 remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
}
add_action( 'init', 'disable_embeds_code_init', 9999 );
function disable_embeds_tiny_mce_plugin($plugins) {
    return array_diff($plugins, array('wpembed'));
}
function disable_embeds_rewrites($rules) {
    foreach($rules as $rule => $rewrite) {
        if(false !== strpos($rewrite, 'embed=true')) {
            unset($rules[$rule]);
        }
    }
    return $rules;

You can use this mu-plugin to dequeue the wp-embed script:

<?php
/*
  Plugin Name: Embed Disabler
  Description: It disable embeds
  Version: 1.0
  Author: TicoLibre
*/
function my_deregister_scripts(){
wp_deregister_script( 'wp-embed' );
}
add_action( 'wp_footer', 'my_deregister_scripts' );

Can I still Embed Posts and YouTube Videos?

If you use the second mu-plugin, you still can embed YouTube Videos but Embedded WordPress will turn into a quote.

If you don’t want to disable embeds, rest assured that your site will still load fast since the scripts is deferred and loaded on the footer.

This is how small the script is:

!(function (d, l) {
    "use strict";
    l.querySelector &&
        d.addEventListener &&
        "undefined" != typeof URL &&
        ((d.wp = d.wp || {}),
        d.wp.receiveEmbedMessage ||
            ((d.wp.receiveEmbedMessage = function (e) {
                var t = e.data;
                if ((t || t.secret || t.message || t.value) && !/[^a-zA-Z0-9]/.test(t.secret)) {
                    for (var s, r, n, a = l.querySelectorAll('iframe[data-secret="' + t.secret + '"]'), o = l.querySelectorAll('blockquote[data-secret="' + t.secret + '"]'), c = new RegExp("^https?:$", "i"), i = 0; i < o.length; i++)
                        o[i].style.display = "none";
                    for (i = 0; i < a.length; i++)
                        (s = a[i]),
                            e.source === s.contentWindow &&
                                (s.removeAttribute("style"),
                                "height" === t.message
                                    ? (1e3 < (r = parseInt(t.value, 10)) ? (r = 1e3) : ~~r < 200 && (r = 200), (s.height = r))
                                    : "link" === t.message && ((r = new URL(s.getAttribute("src"))), (n = new URL(t.value)), c.test(n.protocol)) && n.host === r.host && l.activeElement === s && (d.top.location.href = t.value));
                }
            }),
            d.addEventListener("message", d.wp.receiveEmbedMessage, !1),
            l.addEventListener(
                "DOMContentLoaded",
                function () {
                    for (var e, t, s = l.querySelectorAll("iframe.wp-embedded-content"), r = 0; r < s.length; r++)
                        (t = (e = s[r]).getAttribute("data-secret")) || ((t = Math.random().toString(36).substring(2, 12)), (e.src += "#?secret=" + t), e.setAttribute("data-secret", t)),
                            e.contentWindow.postMessage({ message: "ready", secret: t }, "*");
                },
                !1
            )));
})(window, document);

It is unminified version of the script, so don’t panic.


CSS Snippets for WordPress Embeds

You can use wp-block-embed but that will target all embeds:

.wp-block-embed {
    display: flex;
    justify-content: center;
    align-items: center;
}

or you can add a class to the “Additional CSS classes” and use that to target it with your CSS

.embed-wp-posts {
    display: flex;
    justify-content: center;
    align-items: center;
}

Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.