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:

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:

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:

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:

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:

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:

Automatic Feed Links

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.

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:

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:

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:

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:

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.