How to disable embed in WordPress

For the performance improvements, you need to pay attention to each part of the website. Even if optimization of one element “unweights” the resource for just a few bytes, you still can have the significant result and speed improvement. In this article, we’ll teach you how to get rid of another resource hog – embed in WordPress.

What is embed and how to use it?

The oEmbed option was introduced in WordPress 4.4. The idea was pretty simple: you could add an external link (for example, a YouTube video or a Twitter post) and WordPress would display its content. It means that instead of plain links you will see the actual content – posts from the social media accounts, YouTube videos, etc. The beauty is that you don’t have to add extra code. For example, if you add a SoundCloud link to the visual editor, you’ll get something like this:

Так выглядит преобразованная ссылка на композицию из SoundCloud в визуальном редакторе WordPress

 

The oEmbed protocol supports not only popular social media platforms but other websites too. You can find the list of all supported resources here.

 

Very useful feature, isn’t it? But many websites don’t actually need it. In this case, it’s better to disable oEmbed. This way you will avoid extra HTTP-request produced for the wp-embed.min.js file. Even though this file is not heavy (<2 Kb), it loads on all pages. And this HTTP-request slows each page a bit.

 

How to disable embed in WordPress

There are several ways to disable embed in WordPress. Choose the method based on your skills and knowledge:

  1. With the plugin.
  2. With the code.

1.Disable embed in one click with a plugin

You can disable embed in one click with the Clearfy plugin. Go to the Performance tab and activate disable Embeds.

How to disable embed in WordPress

This plugin has a lot of other cool options. Clearfy is the free and powerful tool that can remove the extra code, fix errors, optimize pages and files, etc. If used correctly, Clearfy can increase the performance of the whole website without extra efforts. Clearfy has more than 40,000 installations and many positive reviews from users worldwide. Download the plugin here.

2.Disable embed manually with the code

If you don’t need embedded content, disable it with the special snippet:

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

Copy this code and add to the bottom of the functions.php file. The file is located in here: Appearance tab => Editor menu).

 

Important: You should be really careful when changing the code. Don’t forget to create a backup copy before changing anything! If you have a lack of tech experience and not confident in your skills, better ask developers for help or use so-called box solutions, such as plugins (see above).

How to optimize the code without disabling embed

If you use embed and want to reduce the amount of code, copy the wp-embed.min.js content to the website code. This way you’ll avoid additional HTTP-requests. However, the solution works for the files with a small amount of code.

Conclusion

If website optimization is important, but you’ve never worked with the code before, use the box solution – Clearfy. It has a great number of features, so you won’t have to download different plugins for different tasks. If you use videos and external materials quite often, then don’t disable embed. This feature will save you a lot of time.