This code snippet allows you to disable embeds in WordPress. Embeds are the feature that allows you to easily embed content from other websites, such as videos, social media posts, or other types of media. Disabling embeds can be useful if you don’t want to allow external content to be embedded on your site or if you want to improve performance by reducing the number of HTTP requests.
The code snippet performs the following actions:
- It removes the REST API endpoint for oEmbed.
- It disables oEmbed auto-discovery.
- It removes the oEmbed filter for parsing oEmbed results.
- It removes oEmbed discovery links from the HTML head.
- It removes oEmbed-specific JavaScript from the front-end and back-end.
- It removes the wpembed TinyMCE plugin, which is responsible for handling oEmbed in the visual editor.
- It removes all embeds rewrite rules.
- It removes the filter for the oEmbed result before any HTTP requests are made.
Add this code to your theme’s functions.php file to disable embeds in WordPress. Remember to test code changes on a staging or local environment before implementing them on a live website.
PHP
/**
* Disable all embeds in WordPress.
*/
add_action( 'init', function () {
// 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', function ( $plugins ) {
return array_diff( $plugins, array( 'wpembed' ) );
} );
// Remove all embeds rewrite rules.
add_filter( 'rewrite_rules_array', function ( $rules ) {
foreach ( $rules as $rule => $rewrite ) {
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
unset( $rules[ $rule ] );
}
}
return $rules;
} );
// Remove filter of the oEmbed result before any HTTP requests are made.
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
}, 9999 );