How to Clean Up WordPress Header

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 in general loving it.

Now I’m converting several client sites to WordPress and one of the things I usually start with is cleaning up a lot of code that I find useless in the document header. All of this code is inserted in the document page through the wp_head() hook.

And how do we clean up WordPress header? I choose to add a function to the WordPress theme being used. I could go with a plugin but I usually find it easier to go with this option. At the same time I’m learning more about how WordPress works.

What code can you remove? I will list first everything that will be removed and then show you how to add this code to a function. But when you apply this code to your theme confirm that you want to remove a specific function.

What we will be removing from wp_head()

  • Windows Live Writer
  • WordPress Generator
  • Really Simple Discovery
  • Shortlink
  • Emojis
  • Relational Links
  • Automatic Feed Links
  • REST API Links

Windows Live Writer

This is the code added by WordPress:

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

This is a function that displays a link to enable support to Windows Live Writer. I don’t need that on my sites so I’ll remove it. Be warned that we are only removing links from our document header. Function will not be disabled in WordPress.

To remove it use this code:

remove_action('wp_head', 'wlwmanifest_link');

WordPress Generator

This is the code added by WordPress:

<meta name="generator" content="WordPress 4.4.2" />

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

To remove it use this code:

remove_action('wp_head', 'wp_generator');

Really Simple Discovery

This is the code added by WordPress:

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

This code displays the link to the Really Simple Discovery service. I don’t integrate services on my sites so it’s safe to remove.

To remove it use this code:

remove_action('wp_head', 'rsd_link');

Shortlink

This is the code added by WordPress:

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

Adds rel=shortlink into head if a shortlink is defined.

To remove it use this code:

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

Emojis

This is the code added by WordPress:

<script type="text/javascript">
			window._wpemojiSettings = {"baseUrl":"https://s.w.org/images/core/emoji/72x72/","ext":".png","source":{"concatemoji":"https://mightywp.com/wp-includes/js/wp-emoji-release.min.js?ver=4.4.2"}};
			!function(a,b,c){function d(a){var c,d=b.createElement("canvas"),e=d.getContext&&d.getContext("2d"),f=String.fromCharCode;return e&&e.fillText?(e.textBaseline="top",e.font="600 32px Arial","flag"===a?(e.fillText(f(55356,56806,55356,56826),0,0),d.toDataURL().length>3e3):"diversity"===a?(e.fillText(f(55356,57221),0,0),c=e.getImageData(16,16,1,1).data.toString(),e.fillText(f(55356,57221,55356,57343),0,0),c!==e.getImageData(16,16,1,1).data.toString()):("simple"===a?e.fillText(f(55357,56835),0,0):e.fillText(f(55356,57135),0,0),0!==e.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g;c.supports={simple:d("simple"),flag:d("flag"),unicode8:d("unicode8"),diversity:d("diversity")},c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.simple&&c.supports.flag&&c.supports.unicode8&&c.supports.diversity||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
		</script>
		<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 .07em !important;
	vertical-align: -0.1em !important;
	background: none !important;
	padding: 0 !important;
}
</style>

Emojis were added to WordPress in version 4.2.0. I never liked that we weren’t given an option to disable emojis on our site inside WordPress admin. So the best option is to disable them using these 2 lines of code that I’ve added to my function.

I never use those and they add too much code. To remove Emojis use this code:

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

Relational Links

This is the code added by WordPress:

<link rel='prev' title='tutorial css' href='https://example.com/tutorial-css/' />
<link rel='next' title='tutorial html5' href='https://example.com/tutorial-html5/' />

This code adds links to prev/next post in html.

To remove it use this code:

remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

Automatic Feed Links

This is the code added by WordPress:

<link rel="alternate" type="application/rss+xml" title="WDV &raquo; Feed" href="https://example.com/feed/" />
<link rel="alternate" type="application/rss+xml" title="WDV &raquo; Comments Feed" href="https://example.com/comments/feed/" />

I like RSS feeds and use them many times including in my mail reader. By default WordPress adds some feeds and usually themes add main feed.

Themes add an action to functions.php so main feed link appears in document header.

add_theme_support( 'automatic-feed-links' );

I prefer to have a cleaner version of feed links because I don’t like feeds created by WordPress. And I can’t disable them one by one (only the main RSS feed) so I prefer to have them disabled in full.

Use this code to make sure all feeds are disabled:

remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);

Don’t forget to add in the HTML link to the main feed. It’s very useful to help readers follow your content.

REST API Links

WordPress 4.4 was the first version to add support for REST API functionality. This is a game changer and important if you are going to use it. But if you don’t have intentions in using it you can remove all that extra code from the document header.

This is the code added by WordPress:

<link rel='https://api.w.org/' href='https://example.com/wp-json/' />

To disable it use this code:

remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );

WordPress also adds oEmbed discovery links with this code:

<link rel="alternate" type="application/json+oembed" href="https://example.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fexample.com%2Fpath%2F" />
<link rel="alternate" type="text/xml+oembed" href="https://example.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fexample.com%2Fpath%2F&#038;format=xml" />

To disable it use this code:

remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

And finally you should also disable link header to every page request:

remove_action('template_redirect', 'rest_output_link_header', 11, 0);

To make sure we remove everything related to oEmbed we should also use the following code:

remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);

In this final code oEmbed results are not filtered, oEmbed REST API route is removed and oEmbed-specific javascript is removed from wp-footer. Example:

<script type='text/javascript' src='https://example.com/wp-includes/js/wp-embed.min.js?ver=4.4.2'></script>

I want to warn you that if you are using REST API then don’t use this code to disable it.

Create a Function

Start by opening your functions.php (always do a backup first of the file that you are going to work on). My function will be called wdv_cleanup. We are going to hook it to after_setup_theme action. This action is one of the first actions to be executed on a page load and it’s used to perform the basic setup for a theme.

Add all of these options combined into this new function:

function wdv_cleanup () {

    remove_action('wp_head', 'wlwmanifest_link'); // remove wlwmanifest.xml (needed to support windows live writer)
    remove_action('wp_head', 'wp_generator'); // remove wordpress version

    remove_action('wp_head', 'rsd_link'); // remove really simple discovery link
    remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); // remove shortlink

    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );  // remove emojis
    remove_action( 'wp_print_styles', 'print_emoji_styles' );   // remove emojis

    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); // remove the / and previous post links

    remove_action('wp_head', 'feed_links', 2); // remove rss feed links
    remove_action('wp_head', 'feed_links_extra', 3); // removes all extra rss feed links

    remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); // remove the REST API link
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); // remove oEmbed discovery links
    remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 ); // remove the REST API link from HTTP Headers

    remove_action( 'wp_head', 'wp_oembed_add_host_js' ); // remove oEmbed-specific javascript from front-end / back-end

    remove_action('rest_api_init', 'wp_oembed_register_route'); // remove the oEmbed REST API route
    remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); // don't filter oEmbed results
}
add_action('after_setup_theme', 'wdv_cleanup');

Please be aware that if you intend in launching your theme to the public, you should not be removing things like Windows Live Writer support for example.

The reason is simple: you never know how someone is going to use your theme and they may need that or other things. And they will contact you requesting support for missing features.

Conclusion

I like to clean up my code as much as possible in WordPress and so I choose to use these options that I have outlined here. Do you use any other functions that also remove unnecessary WordPress code?

Advertisement