How to Disable Embeds in WordPress Without a Plugin
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
<?php
// Function to disable WordPress embeds
function wp_dudecom_disable_embeds() {
// Remove the REST API endpoint for oEmbed
remove_action('rest_api_init', 'wp_oembed_register_route');
// Turn off oEmbed auto discovery
add_filter('embed_oembed_discover', '__return_false');
// Remove oEmbed-specific JavaScript from the front-end and back-end
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
// Remove all embeds rewrite rules
add_filter('rewrite_rules_array', 'wp_dudecom_disable_embeds_rewrites');
// Remove oEmbed from TinyMCE
add_filter('tiny_mce_plugins', 'wp_dudecom_disable_embeds_tinymce');
// Remove oEmbed from the content filter
remove_filter('the_content', array($GLOBALS['wp_embed'], 'autoembed'), 8);
}
// Function to remove embeds rewrite rules
function wp_dudecom_disable_embeds_rewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, 'embed=true')) {
unset($rules[$rule]);
}
}
return $rules;
}
// Function to remove oEmbed from TinyMCE
function wp_dudecom_disable_embeds_tinymce($plugins) {
return array_diff($plugins, array('wpembed'));
}
// Hook the function to init
add_action('init', 'wp_dudecom_disable_embeds', 9999);
?>
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:
- Log in to your WordPress admin dashboard.
- 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.
- In the Theme Editor, locate the functions.php file in the right-hand sidebar under Theme Files.
- Click on functions.php to open it for editing.
- Scroll to the bottom of the file and paste the provided code snippet.
- Click Update File to save your changes.
- 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.