The provided code snippet disables emojis in WordPress. This could be useful in cases where you want to improve site performance by reducing unnecessary HTTP requests, or simply because you don’t use emojis on your site.
This script does a number of things:
- The
print_emoji_detection_script
action is removed from both thewp_head
andadmin_print_scripts
hooks, stopping the script that detects if the browser supports emojis from being printed. - The
print_emoji_styles
action is removed from both thewp_print_styles
andadmin_print_styles
hooks, stopping the CSS for emojis from being printed. - The
wp_staticize_emoji
filter is removed from thethe_content_feed
andcomment_text_rss
hooks, stopping emojis from being turned into static images in the RSS feed. - The
wp_staticize_emoji_for_email
filter is removed from thewp_mail
hook, stopping emojis from being turned into static images in emails. - A new callback function is added to the
tiny_mce_plugins
filter which removes thewpemoji
plugin from TinyMCE, which is used for the post editor. - A new callback function is added to the
wp_resource_hints
filter which removes the URL for the emoji SVGs from the list of URLs to prefetch for performance.
Add this code to your theme’s functions.php file to disable emojis in WordPress. Note that you should test this on a staging or local version of your site before implementing it on a live website.
PHP
/**
* Disable the emojis in WordPress.
*/
add_action( 'init', function () {
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' );
// Remove from TinyMCE.
add_filter( 'tiny_mce_plugins', function ( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
} );
// Remove from dns-prefetch.
add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}, 10, 2 );
} );