WordPress

Cleaning the WordPress head and improving performance

Published on 27. October, 2020

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:

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.

Cleaning the WordPress head and improving performance Ursus Minor

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://blog.shortpixel.com/?p=5503.

As you can imagine, there’s no need to have this in the code if you are using “pretty” links like https://blog.shortpixel.com/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 homepage!' ) ); } 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.

Cleaning the WordPress head and improving performance Ursus Minor

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 );

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 homepage!' ) ); } 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 );

Is your website still slow? Find out here why.

Original article Published here >

Cleaning the WordPress head and improving performance

* Optimise WP Website Images with ShortPixel – Super Fast Website, Optimised SEO, WordPress Plugin (Referral Ad)