Search

How to Disable Embeds in WordPress (without a Plugin)

You may have seen Youtube videos or some tweets displayed on third-party sites. If WordPress powers the site, chances are they are using the oEmbed feature that WordPress has merged since WordPress 4.4.

The oEmbed feature allows users to embed content from another third-party site with just a URL. You just need to place the source URL in the content area, then WordPress will automatically turn it into an embed and display it on your page as a live preview of the source.

If you think you don’t need the oEmbed feature for your WordPress site, you may want to disable the feature to improve your site’s overall load time since every time you load a page, it generates an additional HTTP request on your WordPress site to load the wp-embed.min.js file. The request itself is sometimes a bigger deal than the content download size because things like these add up over time.

In this tutorial, we will show you how to optimize your WordPress site by disabling the embed features.

Disabling Embeds in WordPress Using Custom Code

Before you get started, you may want to backup your site and use a child theme to avoid breaking the parent theme or losing your changes when you update your theme.

Once you’re ready, go to Appearance -> Theme File Editor from your WordPress admin dashboard, then proceed to open up the function.php file.

Once you have opened up the file, then add the following custom code to the child theme (or parent theme if you don’t use a child theme) function.php file.

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;
}

Alternatively, you can disable the embed script by using the wp_dequeue_script function for it. And the code is as follows.

function my_deregister_scripts(){
 wp_dequeue_script( 'wp-embed' );
}
add_action( 'wp_footer', 'my_deregister_scripts' );
This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Picture of Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to save yearly expense up to $219? why not?

Leave a Comment