{"id":5503,"date":"2020-10-27T13:25:20","date_gmt":"2020-10-27T13:25:20","guid":{"rendered":"https:\/\/shortpixel.com/blog\/?p=5503"},"modified":"2023-12-20T13:28:39","modified_gmt":"2023-12-20T11:28:39","slug":"cleaning-the-wordpress-head-and-improving-performance","status":"publish","type":"post","link":"https:\/\/shortpixel.com\/blog\/cleaning-the-wordpress-head-and-improving-performance\/","title":{"rendered":"Cleaning the WordPress head and improving performance"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>What that means is that <strong>most of us could get rid of useless code<\/strong> and therefore save a few bytes from our pages. So we will be effectively <strong>improving our site speed<\/strong>!<\/p>\n\n\n\n<p>Let&#8217;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.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Everything we will talk about involves editing one file, the <strong><em>functions.php<\/em><\/strong> file, which is included inside every theme folder, on the path <em>\/wp-content\/themes\/your_theme\/functions.php<\/em>. 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&#8217;s <em>functions.php<\/em>, 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 <a rel=\"noreferrer noopener\" aria-label=\"this article by WPBeginner (opens in a new tab)\" href=\"https:\/\/www.wpbeginner.com\/wp-themes\/how-to-create-a-wordpress-child-theme-video\/\" target=\"_blank\">this article by WPBeginner<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Removing WordPress version number<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"611\" height=\"107\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/08\/Screenshot-20200812-123804.jpg\" alt=\"\" class=\"wp-image-5504\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/08\/Screenshot-20200812-123804.jpg 611w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/08\/Screenshot-20200812-123804-300x53.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/08\/Screenshot-20200812-123804-604x107.jpg 604w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/figure>\n\n\n\n<p>To remove it, just add the following line to your <em>functions.php<\/em> file, just before the closing &#8220;?&gt;&#8221; tag (if it exists).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action( 'wp_head', 'wp_generator' );\nadd_filter( 'the_generator', '__return_null' );<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Removing WordPress emojis<\/h2>\n\n\n\n<p>Since version 4.2, WordPress added support for emojis for older browsers, because these don&#8217;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&#8217;t need such support anymore, as almost everybody is using a modern browser.<\/p>\n\n\n\n<p>The patch added by WordPress makes your visitors to load an extra JavaScript file to your site (<em>wp-emoji-release.min.js<\/em>).<\/p>\n\n\n\n<p>To disable it, edit your <em>functions.php<\/em> file and add the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Disabling emojis *\/\nremove_action( 'wp_head', 'print_emoji_detection_script', 7 );\nremove_action( 'wp_print_styles', 'print_emoji_styles' );\nremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );\nremove_action( 'admin_print_styles', 'print_emoji_styles' );\n\n\/* Removing s.w.org DNS prefetch *\/\nadd_filter('wp_resource_hints', function (array $urls, string $relation): array {\n    if ($relation !== 'dns-prefetch') {\n        return $urls;\n    }\n    $urls = array_filter($urls, function (string $url): bool {\n        return strpos($url, 's.w.org') === false;\n    });\n    return $urls;\n}, 10, 2);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing wlwmanifest.xml<\/h2>\n\n\n\n<p>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&#8217;s get rid of it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"604\" height=\"42\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/10\/Screenshot_2020-10-21-15_31_20.jpg\" alt=\"\" class=\"wp-image-5559\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot_2020-10-21-15_31_20.jpg 604w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot_2020-10-21-15_31_20-300x21.jpg 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/figure>\n\n\n\n<p>Edit your <em>functions.php<\/em> and add the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action( 'wp_head', 'wlwmanifest_link' );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing the RSD link<\/h2>\n\n\n\n<p>Quoting Wikipedia, &#8220;Really Simple Discovery&nbsp;(RSD) is an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/XML\" target=\"_blank\" rel=\"noopener\">XML<\/a>&nbsp;format and a publishing convention for making services exposed by a&nbsp;blog, or other web software, discoverable by client software&#8221;.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Creating new posts using TextMate, Flock, Thunderbird and other apps<\/li>\n\n\n\n<li>Receiving pingbacks and trackbacks from other sites.<\/li>\n\n\n\n<li>Using Jetpack.<\/li>\n\n\n\n<li>Integrating services like Flickr, so you can receive pictures from there to your WordPress<\/li>\n<\/ul>\n\n\n\n<p>If you don&#8217;t know what all this means, if you don&#8217;t use any third party integration or if you are sure you don&#8217;t need the XML-RPC functionality, you can get rid of the RSD link on your WordPress code by adding the following to your <em>functions.php<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action('wp_head', 'rsd_link');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing shortlink URL<\/h2>\n\n\n\n<p>A shortlink is, as the name suggests, a shorter version of your page links. For example, this article&#8217;s shortlink is <a href=\"https:\/\/shortpixel.com/blog\/?p=5503\">https:\/\/shortpixel.com/blog\/?p=5503<\/a>.<\/p>\n\n\n\n<p>As you can imagine, there&#8217;s no need to have this in the code if you are using &#8220;pretty&#8221; 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.<\/p>\n\n\n\n<p>So, to remove the shortlink, add this to the <em>functions.php<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing RSS feed links<\/h2>\n\n\n\n<p>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, <a href=\"https:\/\/www.wpbeginner.com\/beginners-guide\/what-is-rss-how-to-use-rss-in-wordpress\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"here&#039;s a good explanation (opens in a new tab)\">here&#8217;s a good explanation<\/a> by WPBeginner.<\/p>\n\n\n\n<p>So, if you don&#8217;t know what RSS feeds are or you know that you can definitely disable them, add the following to the <em>functions.php<\/em> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function itsme_disable_feed() {\n wp_die( __( 'Nothing here! Please go back to the &lt;a href=\"'. esc_url( home_url( '\/' ) ) .'\"&gt;homepage&lt;\/a&gt;!' ) );\n}\nadd_action('do_feed', 'itsme_disable_feed', 1);\nadd_action('do_feed_rdf', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss2', 'itsme_disable_feed', 1);\nadd_action('do_feed_atom', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);\nadd_action('do_feed_atom_comments', 'itsme_disable_feed', 1);<\/code><\/pre>\n\n\n\n<p>But that will not remove the links to the RSS feeds that are automatically added by WordPress in the head of your HTML code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"109\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/10\/Screenshot-20201021-141538-1024x109.jpg\" alt=\"\" class=\"wp-image-5557\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot-20201021-141538-1024x109.jpg 1024w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot-20201021-141538-300x32.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot-20201021-141538-768x82.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/10\/Screenshot-20201021-141538.jpg 1426w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To do that, you also have to add the following two lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_action( 'wp_head', 'feed_links_extra', 3 );\nremove_action( 'wp_head', 'feed_links', 2 );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Removing the duplicated robots meta tag<\/h2>\n\n\n\n<p>From WordPress 5.7, the developer team added a <a href=\"https:\/\/make.wordpress.org\/core\/2021\/02\/19\/robots-api-and-max-image-preview-directive-in-wordpress-5-7\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"new function wp_robots() function (opens in a new tab)\">new function wp_robots() function<\/a> which allows to play with the robots meta tag.<\/p>\n\n\n\n<p>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 <em>max-image-preview:large<\/em>, and that&#8217;s fine if it&#8217;s the <strong>only<\/strong> 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.<\/p>\n\n\n\n<p>To check if it&#8217;s your case, simply open your homepage, press Ctrl+U to open the source code and then Ctrl+F to look for the string &#8220;robots&#8221;. You should only find one. Here&#8217;s an example with duplicated robots tags:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"578\" height=\"244\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2021\/04\/Screenshot_20210426_124253.jpg\" alt=\"\" class=\"wp-image-5781\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/04\/Screenshot_20210426_124253.jpg 578w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2021\/04\/Screenshot_20210426_124253-300x127.jpg 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<p>To disable the robots tag added by WordPress, simply add the following to your functions.php:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>remove_filter('wp_robots', 'wp_robots_max_image_preview_large');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Cleaning the WordPress head in one step<\/h2>\n\n\n\n<p>Now, if you want to go ahead and remove everything, here&#8217;s the full code you need to add to the <em>functions.php<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Removing WordPress version*\/\nremove_action( 'wp_head', 'wp_generator' );\nadd_filter( 'the_generator', '__return_null' );\n\n\/* Disabling emojis *\/\nremove_action( 'wp_head', 'print_emoji_detection_script', 7 );\nremove_action( 'wp_print_styles', 'print_emoji_styles' );\nremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );\nremove_action( 'admin_print_styles', 'print_emoji_styles' );\n\n\/* Removing s.w.org DNS prefetch *\/\nadd_filter('wp_resource_hints', function (array $urls, string $relation): array {\n    if ($relation !== 'dns-prefetch') {\n        return $urls;\n    }\n    $urls = array_filter($urls, function (string $url): bool {\n        return strpos($url, 's.w.org') === false;\n    });\n    return $urls;\n}, 10, 2);\n\n\/* Removing wlwmanifest.xml *\/\nremove_action( 'wp_head', 'wlwmanifest_link' );\n\n\/* Removing RSD *\/\nremove_action('wp_head', 'rsd_link');\n\n\/* Removing shortlink *\/\nremove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );\n\n\/* Disabling RSS feeds and removing RSS feed links *\/\nfunction itsme_disable_feed() {\n wp_die( __( 'Nothing here! Please go back to the &lt;a href=\"'. esc_url( home_url( '\/' ) ) .'\"&gt;homepage&lt;\/a&gt;!' ) );\n}\nadd_action('do_feed', 'itsme_disable_feed', 1);\nadd_action('do_feed_rdf', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss2', 'itsme_disable_feed', 1);\nadd_action('do_feed_atom', 'itsme_disable_feed', 1);\nadd_action('do_feed_rss2_comments', 'itsme_disable_feed', 1);\nadd_action('do_feed_atom_comments', 'itsme_disable_feed', 1);\nremove_action( 'wp_head', 'feed_links_extra', 3 );\nremove_action( 'wp_head', 'feed_links', 2 );\n\n\/* Removing duplicated robots tag *\/\nremove_filter('wp_robots', 'wp_robots_max_image_preview_large');<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Is your website still slow?&nbsp;<a href=\"https:\/\/shortpixel.com\/lp\/wso\/?utm_source=BLOG\">Find out here why.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":5564,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-5503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-image-optimization"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/comments?post=5503"}],"version-history":[{"count":12,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5503\/revisions"}],"predecessor-version":[{"id":9338,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5503\/revisions\/9338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media\/5564"}],"wp:attachment":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media?parent=5503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/categories?post=5503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/tags?post=5503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}