Cleaning Up WordPress Head for Improved 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 hostingHosting Hosting is the service that allows you to store your website online so that others can access it. It's like renting a space on the internet to make your site available 24/7. 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 JavaScriptJS JavaScript, abbreviated as "JS," is a programming language used to create interactivity on websites. It allows you to add elements like animations, dynamic forms, and real-time updates without needing to reload the page. JavaScript, like CSS, can be inserted directly into the HTML code or can be added as external .js files referenced by the HTML code. 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.

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://accelerawp.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://accelerawp.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 <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 HTMLHTML HTML is the fundamental language used to create and structure content on web pages. It consists of a set of tags or codes used to describe a web page's structure and content. Each HTML tag has a specific function, like indicating headings, paragraphs, images, links, and other visual elements on a page. Through these tags, web browsers interpret and display content in an organized and understandable manner for users. HTML is essential for building any web page and provides the foundation upon which other elements and styles are added to achieve the desired design. code.

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:

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

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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