Remove the unnecessary junk from WP head

If there is anyway to secure your WordPress site more than what it might be already it’s good to do so, right? One such way to keep your site secure is to hide which version of Wordpress you’re running. Fortunately for any attackers they can see the version number in your <head> code and exploit known vulnerabilities for that Wordpress version.

So a tydy solution is to remove that version number, along with the other unnecessary link information WordPress vomits in to your <head> section, whenever the wp_head() hook gets called. Here’s a real simple chunk of code you can add to your themes functions.php file to fix just that. Edit the lines for removing feeds, some sites do not require a comments rss feed so you’ll need to manually add your main post feed back in to the <head> section manually by providing feed link in your header.php file.

// Remove that junk from my wp_head
 remove_action('wp_head', 'rsd_link'); // Removes the Really Simple Discovery link
 remove_action('wp_head', 'wlwmanifest_link'); // Removes the Windows Live Writer link
 remove_action('wp_head', 'wp_generator'); // Removes the WordPress version
 remove_action('wp_head', 'feed_links', 2); // Removes the RSS feeds remember to add post feed maunally (if required) to header.php
 remove_action('wp_head', 'feed_links_extra', 3); // Removes all other RSS links
 remove_action('wp_head', 'index_rel_link'); // Removes the index page link
 remove_action('wp_head', 'start_post_rel_link', 10, 0); // Removes the random post link
 remove_action('wp_head', 'parent_post_rel_link', 10, 0); // Removes the parent post link
 remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // Removes the next and previous post links

Add the post rss back to your <head> section by adding this line in your themes header.php file

<link rel="alternate" type="application/rss+xml" title="RSS 2.0 Feed" href="<?php bloginfo('rss2_url'); ?>" />

I didn’t come up with this snippet but, it is my own adapted version influenced by what Jeff Starr has written over at Digging In to WordPress… Enjoy