Snippet

How to Disable Embeds in WordPress Without a Plugin

How to disable embeds in wordpressDisable wordpress embeds without pluginWordpress disable oembed featureRemove embeds from wordpress siteDisable embeds wordpress pluginTurn off embeds in wordpressStop wordpress from embedding contentWordpress disable embed javascriptHow to remove oembed in wordpressDisable media embeds wordpress

Explanation

If you're looking to stop WordPress from automatically embedding content like videos or tweets, this code snippet is your go-to solution. Here's what it does:

  • Stops oEmbed Discovery: It prevents WordPress from automatically finding and embedding content from URLs you paste into your posts.
  • Removes oEmbed JavaScript: It takes out the JavaScript that WordPress uses to handle these embeds, both on the front-end (what visitors see) and back-end (where you edit your site).
  • Clears Embed Rewrite Rules: It gets rid of any URL rules that WordPress uses to handle embeds, ensuring they don't sneak back in.
  • Disables oEmbed in TinyMCE: It removes the embed functionality from the WordPress editor, so you won't accidentally add embeds while editing.
  • Disables Auto-Embedding in Content: It stops WordPress from automatically embedding content when you paste a URL directly into your post content.

By adding this code to your theme's functions file, you effectively turn off WordPress's automatic embedding features, giving you more control over what appears on your site. Remember, this doesn't delete existing embeds; it just stops new ones from being added automatically.

Code

1<?php 2// Function to disable WordPress embeds 3function wp_dudecom_disable_embeds() { 4 // Remove the REST API endpoint for oEmbed 5 remove_action('rest_api_init', 'wp_oembed_register_route'); 6 7 // Turn off oEmbed auto discovery 8 add_filter('embed_oembed_discover', '__return_false'); 9 10 // Remove oEmbed-specific JavaScript from the front-end and back-end 11 remove_action('wp_head', 'wp_oembed_add_discovery_links'); 12 remove_action('wp_head', 'wp_oembed_add_host_js'); 13 14 // Remove all embeds rewrite rules 15 add_filter('rewrite_rules_array', 'wp_dudecom_disable_embeds_rewrites'); 16 17 // Remove oEmbed from TinyMCE 18 add_filter('tiny_mce_plugins', 'wp_dudecom_disable_embeds_tinymce'); 19 20 // Remove oEmbed from the content filter 21 remove_filter('the_content', array($GLOBALS['wp_embed'], 'autoembed'), 8); 22} 23 24// Function to remove embeds rewrite rules 25function wp_dudecom_disable_embeds_rewrites($rules) { 26 foreach ($rules as $rule => $rewrite) { 27 if (false !== strpos($rewrite, 'embed=true')) { 28 unset($rules[$rule]); 29 } 30 } 31 return $rules; 32} 33 34// Function to remove oEmbed from TinyMCE 35function wp_dudecom_disable_embeds_tinymce($plugins) { 36 return array_diff($plugins, array('wpembed')); 37} 38 39// Hook the function to init 40add_action('init', 'wp_dudecom_disable_embeds', 9999); 41?>

Instructions

To disable WordPress embeds using the provided code, follow these steps:

File Location: functions.php in your active theme's directory.

Prerequisites:

  • Ensure you have access to your WordPress theme files, either via FTP or a file manager in your hosting control panel.
  • Have a basic understanding of how to edit PHP files.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor. If you don't see this option, you might need to access your files via FTP or your hosting control panel.
  3. In the Theme Editor, locate the functions.php file in the right-hand sidebar under Theme Files.
  4. Click on functions.php to open it for editing.
  5. Scroll to the bottom of the file and paste the provided code snippet.
  6. Click Update File to save your changes.
  7. Clear your site cache if you have a caching plugin enabled to ensure changes take effect immediately.

By following these steps, you will disable WordPress's automatic embedding features, giving you more control over your site's content. If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.