To disable embeds in WordPress, you can use the wp_deregister_script()
function to remove the embed script and the wp_dequeue_script()
function to remove the embed script from the front end. Here’s an example code snippet that you can use:
function wpsnippets_disable_embeds() {
// Remove the embed script
wp_deregister_script( 'wp-embed' );
// Remove the embed script from the front end
wp_dequeue_script( 'wp-embed' );
}
add_action( 'wp_footer', 'wpsnippets_disable_embeds' );
This code snippet will disable the embeds functionality in WordPress by removing the embed script from the footer of your website. You can add this code to your theme’s functions.php
file or in a custom plugin.
By disabling embeds, you can prevent others from embedding your content on their websites, which can help protect your content and maintain control over its distribution. Additionally, disabling embeds can improve the performance of your website by reducing the number of external requests made to load the embed script.
Examples
Example 1: Disable Embeds using a Plugin
This example demonstrates how to disable embeds in WordPress by using a plugin. The code snippet below shows an example of a custom plugin that disables embeds by removing the necessary filters and actions.
<?php
/**
* Plugin Name: Disable Embeds
* Description: Disables embeds in WordPress.
* Version: 1.0
* Author: Your Name
*/
function wpsnippets_disable_embeds_init() {
// Remove the embeds rewrite rule
add_filter('rewrite_rules_array', 'wpsnippets_disable_embeds_rewrites');
// Disable oEmbed discovery links
add_filter('embed_oembed_discover', '__return_false');
// Remove the REST API endpoint
remove_action('rest_api_init', 'wp_oembed_register_route');
// Remove the oEmbed REST API endpoint
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10);
// Disable oEmbed auto discovery
remove_filter('wp_head', 'wp_oembed_add_discovery_links');
// Disable oEmbed-specific JavaScript from the front-end and back-end
remove_action('wp_head', 'wp_oembed_add_host_js');
}
function wpsnippets_disable_embeds_rewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, 'embed=true')) {
unset($rules[$rule]);
}
}
return $rules;
}
add_action('init', 'wpsnippets_disable_embeds_init');
The above code creates a custom plugin that disables embeds in WordPress. It achieves this by removing the necessary filters, actions, and rewrite rules associated with embeds. The wpsnippets_disable_embeds_init()
function is hooked to the init
action and removes various embed-related functionality. The wpsnippets_disable_embeds_rewrites()
function removes the embeds rewrite rule. This plugin can be activated to effectively disable embeds on a WordPress site.
Example 2: Disable Embeds using a Theme Functions File
This example demonstrates how to disable embeds in WordPress by adding code to the theme’s functions.php
file. The code snippet below shows an example of how to disable embeds by removing the necessary filters and actions directly in the theme’s functions file.
<?php
function wpsnippets_disable_embeds_init() {
// Remove the embeds rewrite rule
add_filter('rewrite_rules_array', 'wpsnippets_disable_embeds_rewrites');
// Disable oEmbed discovery links
add_filter('embed_oembed_discover', '__return_false');
// Remove the REST API endpoint
remove_action('rest_api_init', 'wp_oembed_register_route');
// Remove the oEmbed REST API endpoint
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10);
// Disable oEmbed auto discovery
remove_filter('wp_head', 'wp_oembed_add_discovery_links');
// Disable oEmbed-specific JavaScript from the front-end and back-end
remove_action('wp_head', 'wp_oembed_add_host_js');
}
function wpsnippets_disable_embeds_rewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, 'embed=true')) {
unset($rules[$rule]);
}
}
return $rules;
}
add_action('init', 'wpsnippets_disable_embeds_init');
The above code can be added to the theme’s functions.php
file to disable embeds in WordPress. It works in a similar way to the plugin example, removing the necessary filters, actions, and rewrite rules associated with embeds. The wpsnippets_disable_embeds_init()
function is hooked to the init
action and removes various embed-related functionality. The wpsnippets_disable_embeds_rewrites()
function removes the embeds rewrite rule.
Example 3: Disable Embeds using a Custom Plugin and .htaccess
This example demonstrates how to disable embeds in WordPress by using a custom plugin and modifying the .htaccess
file. The code snippet below shows an example of a custom plugin that disables embeds by removing the necessary filters and actions, and also includes instructions to modify the .htaccess
file.
<?php
/**
* Plugin Name: Disable Embeds
* Description: Disables embeds in WordPress.
* Version: 1.0
* Author: Your Name
*/
function wpsnippets_disable_embeds_init() {
// Remove the embeds rewrite rule
add_filter('rewrite_rules_array', 'wpsnippets_disable_embeds_rewrites');
// Disable oEmbed discovery links
add_filter('embed_oembed_discover', '__return_false');
// Remove the REST API endpoint
remove_action('rest_api_init', 'wp_oembed_register_route');
// Remove the oEmbed REST API endpoint
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10);
// Disable oEmbed auto discovery
remove_filter('wp_head', 'wp_oembed_add_discovery_links');
// Disable oEmbed-specific JavaScript from the front-end and back-end
remove_action('wp_head', 'wp_oembed_add_host_js');
}
function wpsnippets_disable_embeds_rewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, 'embed=true')) {
unset($rules[$rule]);
}
}
return $rules;
}
add_action('init', 'wpsnippets_disable_embeds_init');
To further enhance the disabling of embeds, you can modify the .htaccess
file. Add the following code to the .htaccess
file in the root directory of your WordPress installation:
# BEGIN Disable Embeds
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /embed/ [NC]
RewriteRule ^.*$ - [F]
</IfModule>
# END Disable Embeds