Cleaning the WordPress head and improving performance

WordPress ships by default with some code that may not be needed in all WordPress sites. This is normal, since WordPress needs to reach to all possible types of websites.

What that means is that most of us could get rid of useless code and therefore save a few bytes from our pages. So we will be effectively improving our site speed!

Let’s first see one by one all parts that can be removed, how to remove them, and at the end of the article we will provide the method to remove everything with a simple copy-paste.

Everything we will talk about involves editing one file, the functions.php file, which is included inside every theme folder, on the path /wp-content/themes/your_theme/functions.php. You can use a file manager to edit it; most of hosting providers have one. It is also a good idea to create a child theme out of your original theme and edit the child’s functions.php, so these changes will not be lost in future updates. If your theme provider does not provide you with a child theme, you can check out this article by WPBeginner.

Removing WordPress version number

This is definitely not needed on the front-end. Nobody should care about our WordPress version. Moreover, it is also a security risk. The code is only this line, and it has no function other than announce our version number:

Screenshot 20200812 123804

To remove it, just add the following line to your functions.php file, just before the closing “?>” tag (if it exists).

remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_null' );

The second line of the previous code will not remove the version from your pages head, but from your RSS feeds, which is nevertheless a nice addition.

Removing WordPress emojis

Since version 4.2, WordPress added support for emojis for older browsers, because these don’t know how to render them appropriately. However, version 4.2 was released in 2015, which means that it is almost guaranteed that you won’t need such support anymore, as almost everybody is using a modern browser.

The patch added by WordPress makes your visitors to load an extra JavaScript file to your site (wp-emoji-release.min.js).

To disable it, edit your functions.php file and add the following:

/* Disabling emojis */
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

/* Removing s.w.org DNS prefetch */
add_filter('wp_resource_hints', function (array $urls, string $relation): array {
    if ($relation !== 'dns-prefetch') {
        return $urls;
    }
    $urls = array_filter($urls, function (string $url): bool {
        return strpos($url, 's.w.org') === false;
    });
    return $urls;
}, 10, 2);

Removing wlwmanifest.xml

WordPress adds by default this line of code, which is only used by Windows Live Writer. We can almost guarantee that you are not using it, so let’s get rid of it.

Screenshot 2020 10 21 15 31 20

Edit your functions.php and add the following line:

remove_action( 'wp_head', 'wlwmanifest_link' );

Removing the RSD link

Quoting Wikipedia, “Really Simple Discovery (RSD) is an XML format and a publishing convention for making services exposed by a blog, or other web software, discoverable by client software”.

In simpler words, it is a mechanism that XML-RPC clients use to be able to work on your website, without you doing any work on the actual site. Some examples are:

  • Creating new posts using TextMate, Flock, Thunderbird and other apps
  • Receiving pingbacks and trackbacks from other sites.
  • Using Jetpack.
  • Integrating services like Flickr, so you can receive pictures from there to your WordPress

If you don’t know what all this means, if you don’t use any third party integration or if you are sure you don’t need the XML-RPC functionality, you can get rid of the RSD link on your WordPress code by adding the following to your functions.php:

remove_action('wp_head', 'rsd_link');

Removing shortlink URL

A shortlink is, as the name suggests, a shorter version of your page links. For example, this article’s shortlink is https://shortpixel.com/blog/?p=5503.

As you can imagine, there’s no need to have this in the code if you are using “pretty” links like /blog/my-test-article/. Moreover, even if you remove the shortlink from the code, the actual shortlink will still work: it will just redirect to the good link, which is in fact the behavior we expect.

So, to remove the shortlink, add this to the functions.php:

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

Removing RSS feed links

Do you know what an RSS feed is? If not, it is safe to remove the RSS links WordPress generates. But for the curious ones, here’s a good explanation by WPBeginner.

So, if you don’t know what RSS feeds are or you know that you can definitely disable them, add the following to the functions.php file:

function itsme_disable_feed() {
 wp_die( __( 'Nothing here! Please go back to the <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
}
add_action('do_feed', 'itsme_disable_feed', 1);
add_action('do_feed_rdf', 'itsme_disable_feed', 1);
add_action('do_feed_rss', 'itsme_disable_feed', 1);
add_action('do_feed_rss2', 'itsme_disable_feed', 1);
add_action('do_feed_atom', 'itsme_disable_feed', 1);
add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);
add_action('do_feed_atom_comments', 'itsme_disable_feed', 1);

But that will not remove the links to the RSS feeds that are automatically added by WordPress in the head of your HTML code.

Screenshot 20201021 141538

To do that, you also have to add the following two lines:

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

Removing the duplicated robots meta tag

From WordPress 5.7, the developer team added a new function wp_robots() function which allows to play with the robots meta tag.

The robots meta tag is a line added to the code of the page providing instructions to search engines for how to crawl or index the website. By default, WordPress adds it with a value of max-image-preview:large, and that’s fine if it’s the only robots meta tag on your site. But if you already have it (thanks to an SEO plugin or because you added it manually), you could end up with duplicated robots meta tags, and that could affect your SEO, as well as adding unnecessary stuff to your code.

To check if it’s your case, simply open your homepage, press Ctrl+U to open the source code and then Ctrl+F to look for the string “robots”. You should only find one. Here’s an example with duplicated robots tags:

Screenshot 20210426 124253

To disable the robots tag added by WordPress, simply add the following to your functions.php:

remove_filter('wp_robots', 'wp_robots_max_image_preview_large');

Cleaning the WordPress head in one step

Now, if you want to go ahead and remove everything, here’s the full code you need to add to the functions.php:

/* Removing WordPress version*/
remove_action( 'wp_head', 'wp_generator' );
add_filter( 'the_generator', '__return_null' );

/* Disabling emojis */
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

/* Removing s.w.org DNS prefetch */
add_filter('wp_resource_hints', function (array $urls, string $relation): array {
    if ($relation !== 'dns-prefetch') {
        return $urls;
    }
    $urls = array_filter($urls, function (string $url): bool {
        return strpos($url, 's.w.org') === false;
    });
    return $urls;
}, 10, 2);

/* Removing wlwmanifest.xml */
remove_action( 'wp_head', 'wlwmanifest_link' );

/* Removing RSD */
remove_action('wp_head', 'rsd_link');

/* Removing shortlink */
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );

/* Disabling RSS feeds and removing RSS feed links */
function itsme_disable_feed() {
 wp_die( __( 'Nothing here! Please go back to the <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
}
add_action('do_feed', 'itsme_disable_feed', 1);
add_action('do_feed_rdf', 'itsme_disable_feed', 1);
add_action('do_feed_rss', 'itsme_disable_feed', 1);
add_action('do_feed_rss2', 'itsme_disable_feed', 1);
add_action('do_feed_atom', 'itsme_disable_feed', 1);
add_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);
add_action('do_feed_atom_comments', 'itsme_disable_feed', 1);
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );

/* Removing duplicated robots tag */
remove_filter('wp_robots', 'wp_robots_max_image_preview_large');

Is your website still slow? Find out here why.

Leave a Reply

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