How to Remove Everything OEmbed from WordPress: A Comprehensive Guide

Home » Snippets » How to Remove Everything OEmbed from WordPress: A Comprehensive Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Embedding content from various sources is a common and useful feature of WordPress. However, there may be situations where you want to remove all oEmbed (embedded media) from your website, perhaps to declutter your pages or improve page load times. If you’re wondering how to do it without gleaning through each post manually, this article will guide you step-by-step on how to remove everything oEmbed from your WordPress site efficiently. Let’s make your pages cleaner and faster together!

					function wpturbo_remove_oembed() {
   // Remove oEmbed-related scripts and styles
   remove_action('wp_head', 'wp_oembed_add_host_js');
   remove_filter('oembed_dataparse', 'wp_filter_pre_oembed_result', 10);
   remove_action('rest_api_init', 'wp_oembed_register_route');
   remove_filter('embed_oembed_discover', '__return_false');
   remove_action('wp_head', 'wp_oembed_add_discovery_links');
   remove_action('wp_head', 'wp_oembed_add_js_api');

   // 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_action('init', 'wpturbo_remove_oembed');
				

Primarily, the function wpturbo_remove_oembed() is created, which houses all the functions to disable oEmbed functionality.

Starting from the top, the actions and filters are removed in the following order:

  1. The remove_action('wp_head', 'wp_oembed_add_host_js') is responsible for removing the JavaScript commonly used by oEmbed in the WordPress header.

  2. remove_filter('oembed_dataparse', 'wp_filter_pre_oembed_result', 10) is a typical WordPress filter used to manipulate the data retrieved from the oEmbed provider before the data gets cached in a post meta. Setting it to remove ensures that the oEmbed data won’t be parsed.

  3. The remove_action('rest_api_init', 'wp_oembed_register_route') disengages the necessary REST API route that oEmbed uses.

  4. remove_filter('embed_oembed_discover', '__return_false') will simply return false to any oEmbed discovery attempts.

  5. The remove_action('wp_head', 'wp_oembed_add_discovery_links') is responsible for removing the discovery links that WordPress automatically injects into the head of your site’s pages.

  6. remove_action('wp_head', 'wp_oembed_add_js_api') removes the oEmbed-specific JavaScript from the frontend.

  7. The second occurrence of remove_action('wp_head', 'wp_oembed_add_discovery_links') and remove_action('wp_head', 'wp_oembed_add_host_js') appears to be duplicates and can be removed as they don’t add any additional functionality.

After defining the function wpturbo_remove_oembed(), add_action('init', 'wpturbo_remove_oembed') then hooks our function onto WordPress ‘init’, ensuring it’s called during the initialization stage of WordPress every time a page is loaded.

To reiterate, this entire snippet is aimed at removing the oEmbed functionality that WordPress comes with by default, minimizing the potential for any issues or conflicts it might have with your themes or plugins in some cases.

Register an account to save your snippets or go Pro to get more features.