Disable wp-emoji and wp-embed

WP Emoji

In case you don’t know, since recent versions (4.4 vicinity) WordPress is including various scripts that slow down your site in the core.

Emoji and Embed scripts.

Each of them include over 2-3 JS and CSS files, and load for every visitor who comes to your WordPress site. Check your WordPress page source through your browser – if you see any styles and scripts with the name wp-emoji-* or wp-embed-*, you have them.

The problem with these scripts is that they collectively add ~0.5 to ~1.5 seconds of time to your page load speeds, depending on your setup, host and connectivity. That’s a big deal in our time and age, in which visitors expect a page to load in about ~2 to ~3 seconds. [1]

Not good.

Especially when you are not using either of these… And in case you don’t know what these are, then you are very probably not using any of them.

Embeds are for ‘easy embedding’ YouTube videos, Twitter Tweets, Audio and ‘other content’ to your posts. [2].

Except, Youtube, Twitter and any decent provider which you would include any such content from, already provide iframe or JavaScript embedding options. And these do not load on every single page – just the page you add them to.

So, you don’t need what WordPress forces with the recent cores.

Then what about Emoji?

They are stuff like these:

WP Emoji
Ever used one on your posts?

Have you used any of these in any of your posts? Why would someone even need to use one in a semi-useful blog post, leave aside why would any developer think that it would be a good idea to include these in WordPress core?

Some say that these scripts will be useful for Asian languages. But hey – in case your website is not catering to any asiatic language, that’s not a point either.

So, its better to just disable these scripts until WordPress team gains the good sense to not include stuff majority of people won’t need in WordPress core.

How to go about doing that?

Just paste the following code to a plugin, and activate it.

<?php
/*
	Plugin Name: CodeBard Disable WP Emoji and WP Embed
	Plugin URI: http://codebard.com
	Description: An easy way to disable WP Emojis and WP Embeds if you are not using them.
	Author: CodeBard
	Author URI: http://codebard.com
*/

// Disables Pesky Emojis

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );


/// Disables Embeds

function cb_disable_peskies_disable_embeds_rewrites( $rules ) {
	foreach ( $rules as $rule => $rewrite ) {
		if ( false !== strpos( $rewrite, 'embed=true' ) ) {
			unset( $rules[ $rule ] );
		}
	}
	return $rules;
}

function cb_disable_peskies_disable_embeds_tiny_mce_plugin( $plugins ) {
	return array_diff( $plugins, array( 'wpembed' ) );
}

function cb_disable_peskies_disable_embeds_remove_rewrite_rules() {
	add_filter( 'rewrite_rules_array', 'cb_disable_peskies_disable_embeds_rewrites' );
	flush_rewrite_rules();
}

function cb_disable_peskies_disable_embeds_flush_rewrite_rules() {
	remove_filter( 'rewrite_rules_array', 'cb_disable_peskies_disable_embeds_rewrites' );
	flush_rewrite_rules();
}


function cb_disable_peskies_disable_embeds()
{

	// 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', 'cb_disable_peskies_disable_embeds_tiny_mce_plugin' );

	// Remove all embeds rewrite rules.
	add_filter( 'rewrite_rules_array', 'cb_disable_peskies_disable_embeds_rewrites' );


}

add_action( 'init', 'cb_disable_peskies_disable_embeds', 99 );
register_activation_hook( __FILE__, 'cb_disable_peskies_disable_embeds_remove_rewrite_rules' );
register_deactivation_hook( __FILE__, 'cb_disable_peskies_disable_embeds_flush_rewrite_rules' );

?>

What these will do is to disable all hooks, filters, queues related to Emojis and Embeds, everywhere. Even speeding up your admin a little bit.

In case you can’t get hassled by popping the above code into a plugin, just download the below ready-made one, upload it to your WordPress and activate it. Its the same code above, except zipped into a plugin. And that’s it.

CodeBard Disable Peskies – WP Emoji & WP Embed

Ah, if you are using our Rapier Theme, you don’t need to use this plugin since this code is included in speed optimizations and you can just turn them on/off in General -> Advanced JS and CSS settings options.

Leave a Reply