How to Clean up WordPress wp_head and Improve Page Speed

I started working with WordPress a couple of years ago when I decided to launch a couple of content sites. Since then, I’ve been working a lot with WordPress and generally loving it.

Why Remove Code from the WordPress Header

WordPress includes code in the WordPress header that most people don’t need. Using a function, wp_head(), a lot of code is added by WordPress core, plugins, and themes.

Much of this code is unnecessary for your site, and removing it will not affect your site most of the time and will help improve your site’s performance.

How can we remove unwanted code?

One of our options is adding code to a file called functions.php which is included with every theme folder. The file can be found at

/wp-content/themes/your-theme/functions.php

What code can you remove from wp_head? I will list the most common ones you can usually remove from your site.

What we will be removing from wp_head()

This is the code that displays a link to enable support for Windows Live Writer. I don’t need that on my sites, so I’ll remove it.

By default, this is the code added by WordPress:

HTML
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="https://mightywp.com/wp-includes/wlwmanifest.xml" />

To remove, add the following code to the functions.php file:

PHP
remove_action('wp_head', 'wlwmanifest_link');

WordPress Generator

This code places the WordPress version being used on your site. That information is unnecessary to anyone else, so I choose to remove it.

By default, this is the code added by WordPress:

HTML
<meta name="generator" content="WordPress 6.3.1" />

To remove, add the following code to the functions.php file:

PHP
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_null');

The second line removes WordPress version information from RSS feeds.

Shortlink adds, as the name indicates, a shorter link to your page. We already have a link to each post, and even if we remove it from the header, it will still work, so I prefer to remove it.

By default, this is the code added by WordPress:

HTML
<link rel="shortlink" href="https://mightywp.com/">

To remove, add the following code to the functions.php file:

PHP
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); 

Really Simple Discovery

This code adds a line with the Really Simple Discovery service link. Third-party services connect to WordPress through this link to work on your site. If you don’t use any third-party integration, it’s safe to assume you can remove it.

By default, this is the code added by WordPress:

HTML
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://mightywp.com/xmlrpc.php?rsd" />

To remove, add the following code to the functions.php file:

PHP
remove_action('wp_head', 'rsd_link');

Robots Meta Tag

In WordPress 5.7, it was added a new function that adds a robots meta tag to the header.

If you use an SEO plugin, you can end with duplicated meta tags. To prevent this, I also removed this one.

By default, this is the code added by WordPress:

HTML
<meta name='robots' content='max-image-preview:large' />

To remove, add the following code to the functions.php file:

PHP
remove_filter('wp_robots', wp_robots_max_image_preview_large');

Emojis

Emojis were added to WordPress in version 4.2.0 to improve support for older browsers.

I never liked that we weren’t allowed to disable emojis on our site inside WordPress admin.

They add too much code, so I always remove them.

By default, this is the code added by WordPress:

JavaScript
<script type="text/javascript">
window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/mightywp.com\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.2"}};
/*! This file is auto-generated */
!function(e,a,t){var n,r,o,i=a.createElement("canvas"),p=i.getContext&&i.getContext("2d");function s(e,t){p.clearRect(0,0,i.width,i.height),p.fillText(e,0,0);e=i.toDataURL();return p.clearRect(0,0,i.width,i.height),p.fillText(t,0,0),e===i.toDataURL()}function c(e){var t=a.createElement("script");t.src=e,t.defer=t.type="text/javascript",a.getElementsByTagName("head")[0].appendChild(t)}for(o=Array("flag","emoji"),t.supports={everything:!0,everythingExceptFlag:!0},r=0;r<o.length;r++)t.supports[o[r]]=function(e){if(p&&p.fillText)switch(p.textBaseline="top",p.font="600 32px Arial",e){case"flag":return s("\ud83c\udff3\ufe0f\u200d\u26a7\ufe0f","\ud83c\udff3\ufe0f\u200b\u26a7\ufe0f")?!1:!s("\ud83c\uddfa\ud83c\uddf3","\ud83c\uddfa\u200b\ud83c\uddf3")&&!s("\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f","\ud83c\udff4\u200b\udb40\udc67\u200b\udb40\udc62\u200b\udb40\udc65\u200b\udb40\udc6e\u200b\udb40\udc67\u200b\udb40\udc7f");case"emoji":return!s("\ud83e\udef1\ud83c\udffb\u200d\ud83e\udef2\ud83c\udfff","\ud83e\udef1\ud83c\udffb\u200b\ud83e\udef2\ud83c\udfff")}return!1}(o[r]),t.supports.everything=t.supports.everything&&t.supports[o[r]],"flag"!==o[r]&&(t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&t.supports[o[r]]);t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&!t.supports.flag,t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.everything||(n=function(){t.readyCallback()},a.addEventListener?(a.addEventListener("DOMContentLoaded",n,!1),e.addEventListener("load",n,!1)):(e.attachEvent("onload",n),a.attachEvent("onreadystatechange",function(){"complete"===a.readyState&&t.readyCallback()})),(e=t.source||{}).concatemoji?c(e.concatemoji):e.wpemoji&&e.twemoji&&(c(e.twemoji),c(e.wpemoji)))}(window,document,window._wpemojiSettings);
</script>
CSS
<style type="text/css">
img.wp-smiley,
img.emoji {
	display: inline !important;
	border: none !important;
	box-shadow: none !important;
	height: 1em !important;
	width: 1em !important;
	margin: 0 0.07em !important;
	vertical-align: -0.1em !important;
	background: none !important;
	padding: 0 !important;
}
</style>

To remove, add the following code to the functions.php file:

PHP
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

Final Notes

If you go ahead and remove code added by WordPress, don’t forget to test everything before adding these changes to your live site.

By optimizing the WordPress head tag, you removed default WordPress actions that are not needed in most case scenarios.

You’ll have a faster website, and your visitor’s experience will improve.

Finally, if you create a WordPress theme and want to launch it publicly, you should not add this code to remove WordPress features.

The reason is simple: you never know how someone will use your theme. They may need some of these features or all of them.