WordPress core ads quite a lot of code in site’s HTML head section whether you want it or not. That code is far from useless and serves a purpose on most sites. However, if you are a clean-code freak or using WordPress to power a SaaS you’ll want to clean up the HTML. As with most things in WP, it’s just a matter of removing a few actions and filters.

Why would I remove those tags in HEAD, CSS or JS files?

There are a couple of reasons. Some people like to keep their code and sites as tidy as possible. Why have a line of code in HTML if you know you don’t need it and know how to remove it. No reason what so ever. Same goes for extra CSS and JS files. If you’re not using emojis on your site why would you include that JS on every page?

Then there’s security. There are a few pieces of HTML that WordPress automatically adds do show URLs that have been exploited in the past. I have to stress out that these URLs (XML RPC endpoint in particular) are not a secret. They should be public. But, changing them to something custom, and hiding them is a known practice. In that case, it’s obvious you don’t want those URLs in your HTML.

Speed is also a valid reason for cleaning up HTML. Although I have to say I’m not sure how much you’ll gain by removing a few hundred characters from HTML, it’s a step in the right directions. Eliminating unneeded JS and CSS files will save you a few HTTP requests, and that’s something you should focus your optimization on. Tidy Repo recently tested 100+ WordPress themes for speed, and the number of requests makes a significant impact on the overall site speed.

Having clean HTML code ensures you know what’s going on your site and is a great start to optimizing the site’s speed.

Last, but not the least, reason and the reason why we have this code handy are WordPress powered SaaSes. When you’re building a SaaS, you want complete control of what’s in your HTML, and the majority of things WP adds to HTML by itself are not something you’ll be using. So, for the sake of all the reasons noted above – we clean up things as much as possible. That gives us a great, lean platform to build custom solutions on, while still keeping 99% of WP’s core functionality that’s used to power the SaaS.

Can I safely remove these things? Don’t they serve a purpose?

Yes, they do serve a purpose, but only in certain situations. On an “average” website a few extra link tags in the header are not a problem, and they all make certain operations and integrations easier. But, they are not crucial to the functioning of any site and certainly not for a SaaS.

Let’s clean up some code

You can test the code below with any theme. The default 2017 will do just fine. However, if you’re using a “normal” theme (and some plugins on top of that), you can never know who added the code – WP core, theme, or a plugin. I suggest you reset your site to remove all plugins and then use a theme that only has wp_head() and wp_footer() in it. Here’s how that index.php looks and here’s the whole test theme you can use for testing.

<!DOCTYPE html>
<html>
<head>
  <?php
    wp_head();
  ?>
</head>
<body>
  <h1>The Most Basic WP Theme</h1>
  <?php
    wp_footer();
  ?>
</body>
</html>

If you activate our test theme and open the front-page source, you’ll notice a whole lot more code than it’s visible in the index.php above. That’s what we’ll be cleaning out. Place the code below in theme’s functions.php. If you don’t want to remove/disable a feature, comment out the line that mentions it.

add_action('after_setup_theme', 'cleanup');

function cleanup() {
  remove_action('wp_head', 'rsd_link');
  remove_action('wp_head', 'wp_generator');
  remove_action('wp_head', 'feed_links', 2); 
  remove_action('wp_head', 'feed_links_extra', 3);
  remove_action('wp_head', 'index_rel_link');
  remove_action('wp_head', 'wlwmanifest_link');
  remove_action('wp_head', 'start_post_rel_link', 10, 0);
  remove_action('wp_head', 'parent_post_rel_link', 10, 0);
  remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
  remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
  remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
  remove_action('wp_head', 'print_emoji_detection_script', 7);
  remove_action('wp_head', 'rel_canonical');
  remove_action('wp_head', 'rel_alternate');
  remove_action('wp_head', 'wp_oembed_add_discovery_links');
  remove_action('wp_head', 'wp_oembed_add_host_js');
  remove_action('wp_head', 'rest_output_link_wp_head');
  
  remove_action('rest_api_init', 'wp_oembed_register_route');
  remove_action('wp_print_styles', 'print_emoji_styles');
  
  remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
  remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10);
  
  add_filter('embed_oembed_discover', '__return_false');
  add_filter('show_admin_bar', '__return_false');
}

What does what

rsd_link action is responsible for displaying the really simple discovery link while wp_generator shows the WP version and is removed by virtually every security plugin ever written for WP. The two feed_links actions add various links to several RSS feeds. Various _rel_link actions add links to posts so they can be more accessible and more easily found by various services. Unless you’re using Windows Live Writer you really don’t need the wlwmanifest_link action. We don’t need amojis so print_emoji_detection_script can go, and the same goes for various oembed links including support for oembed in REST API. Removing the oembed_dataparse action will make sure WP doesn’t mess with any links in your posts and doesn’t turn them into any rich-media via oembed. Last filter show_admin_bar will prevent the WP admin bar from showing on your front-end regardless of per-account settings.

WordPress flexibility is great

Many articles are floating around with titles such as “How to remove junk from WP header.” It’s not junk. It’s stuff that gives great power to WordPress straight out-of-the-box. Many people value that, and they don’t have to spend hours setting things up. On the other hand, cleaning is just a matter of removing a few actions. You have full control over what gets put into your HTML and can easily use WordPress to power non-content sites such as SaaSes.

    1. Hi, had that intention but turned into quite a long text 🙂 So I decided to cut it up a bit into the “What does what” paragraph.


Leave a Reply

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