How to clean the WordPress head

Here is an explanation, with examples, of how to clean the WordPress head in one step by removing the WordPress version number, disabling emojis, removing WLWManifest.xml, removing RSD link, removing shortlink URL, removing RSS feed links, and removing the duplicated robots meta tag:

Removing WordPress Version Number

The WordPress version number is a security risk because it can be used by hackers to identify vulnerable sites. To remove the WordPress version number from the head of your website, you can add the following code to your functions.php file:

PHP

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

Disabling Emojis

Emojis can be slow to load and can make your website look cluttered. To disable emojis from your website, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'print_emoji_detection_script');
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 WLWManifest.xml

WLWManifest.xml is a file that was used by Windows Live Writer to publish posts to WordPress.com. It is no longer necessary and can be removed from your website’s head. To remove WLWManifest.xml, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'wlwmanifest_link');

Removing RSD Link

RSD stands for Really Simple Discovery. It is a protocol for search engines to find websites that have a blog. The RSD link is no longer necessary and can be removed from your website’s head. To remove the RSD link, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'rsd_link');

Removing Shortlink URL

The shortlink URL is a shortened version of your website’s permalink. It is not necessary to display the shortlink URL in the head of your website. To remove the shortlink URL, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'wp_shortlink_wp_head');

Removing RSS Feed Links

RSS feeds are used to subscribe to content from a website. You can remove the RSS feed links from your website’s head if you don’t want people to subscribe to your RSS feed. To remove the RSS feed links, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'feed_links_extra');

Removing Duplicated Robots Meta Tag

The robots meta tag tells search engines which pages on your website they should index. If you have multiple robots meta tags in your header, it can confuse search engines. To remove duplicate robots meta tags, you can add the following code to your functions.php file:

PHP

remove_action('wp_head', 'print_robots');

By following these steps, you can clean the WordPress head of your website and improve its performance and security.

Cleaning 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 or add as plugin:

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

Author

  • Imants Pumpurs

    Author is a Latvian writer, playwright and screenwriter. He is considered one of the most important Latvian writers of the 21st century. His work is known for its humor and its social satire.

EN