Clean up and Optimize WordPress Header Code

Notice a lot of extra code that appears unnecessary or “junk” in your website’s page source? As an advocate of clean and minimal page code ( pro-SEO, to some extent ) I like to optimize every single piece of code and keep things as clean and efficient as possible.

Let’s look at some of that seemingly unwanted stuff and an easy way to get rid of them.

Unnecessary junk in WordPress site’s header

Here’s an example of a WordPress website’s header with those extra rel, or meta lines of code. Each of these lines has a purpose. I, however, do not find value in the rel tags and their purpose and clean them up first thing post a fresh install. While that may be the case with you; take some time to understand what each line does and feel free to follow the steps to clean them all (or some).

unnecessary code in wordpress header

Let me explain a few of them:

Really Simple Discovery (RSD)


I’m talking about the code that generates this line in your WordPress blog:

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

This is an XML format and a publishing convention for making services exposed by your blog discoverable by client software. If you’ve no idea what this is, probably you’re not going to use it for connecting client software to your blog.

WP Generator

This displays the version of WordPress you’re using.

<meta name="generator" content="WordPress 5.4.1" />
Post Relational Links

Post relational links are, as the name suggests, the information about the relationship with some other pages. I didn’t really see a huge benefit from them.

Similarly, there are tags related to Feed Links and other stuff that aren’t really something useful to the vast majority of websites. However, there are some tags you shouldn’t remove, such as:

Canonical Tag

I would recommend you retain the canonical tag that outputs something like this:

<link rel="canonical" href="https://www.bloghow.com/how-to-start-a-blog.html" />

Canonical tags help avoid duplicate content issues with SEO. It helps search engines understand identical content under different URLs and helps them better index one version of the multiple available. We will not remove that.

Here’s the code snippet you need to place in your theme’s functions.php file. Paste the code in a new line after the beginning <?php

//BEGIN Remove Junk From Header - BlogHow.com 
remove_action('wp_head', 'rsd_link'); // remove really simple discovery
remove_action('wp_head', 'wp_generator'); // remove wordpress version
remove_action('wp_head', 'feed_links', 2); // remove rss feed links
remove_action('wp_head', 'feed_links_extra', 3); // removes all rss feed links
remove_action('wp_head', 'index_rel_link'); // removes link to index page
remove_action('wp_head', 'wlwmanifest_link'); // remove wlwmanifest.xml
remove_action('wp_head', 'start_post_rel_link', 10, 0); // remove random post link
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // remove parent post link
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // remove the next and previous post links
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); // remove shortlink
remove_action('wp_head', 'rest_output_link_wp_head', 10);// Disable REST API link tag
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);// Disable oEmbed Discovery Links
remove_action('template_redirect', 'rest_output_link_header', 11, 0);// Disable REST API link in HTTP headers
//END Remove Junk From Header - BlogHow.com 

There might be additional pieces of “junk” you don’t like bloating up the page code. One such block of code is related to how Emojis are displayed on your website. If you don’t care about or your blog doesn’t use of a lot of emojis, you should follow the instructions to remove emojis code from WordPress head; and remove those pieced of code as well.

Conclusion

Just like with everything else with WordPress, you could instead use a plugin to clean up the unnecessary code from your WordPress site’s HEAD. However, I would rather follow the steps above and minimize the use of 3rd-party plugins on your website for these petty things. After all, a lean and clean website is all we want right?

Did you encounter other possible lines of unwanted code in the WordPress header? Or, need help cleaning things up a bit more? Let us know in the comments below.

2 Comments on “Clean up and Optimize WordPress Header Code”

Leave a Reply

Your email address will not be published. Required fields are marked *