Remove head tag bloat from WordPress using functions.php

WordPress comes jam packed with things in the <head> tag that I almost never want. For example:

  • xmlrpc.php
  • wlwmanifest.xml
  • application/json+oembed
  • wp-json
  • Gutenberg block editor things such as classic-themes.min.css, wp-block-library-css, global-styles-inline-css, and a ton of svg elements after the opening <body> tag
  • Default favicons (I add these back my own way)
  • Emojis 😝

Removing these things can be done by adding the following to your functions.php file.

functions.php

<pre class="wp-block-code"><code class="language-php">// ***********************************
//  Remove Bloat
// ***********************************

// Remove various bloat:
// &lt;link rel=&quot;EditURI&quot; type=&quot;application/rsd+xml&quot; title=&quot;RSD&quot; href=&quot;http://domain.com/xmlrpc.php?rsd&quot; /&gt;
// &lt;link rel=&quot;wlwmanifest&quot; type=&quot;application/wlwmanifest+xml&quot; href=&quot;http://domain.com/wp-includes/wlwmanifest.xml&quot; /&gt;
// &lt;link rel=&quot;alternate&quot; type=&quot;application/json+oembed&quot; href=&quot;http://domain.com/wp-json/oembed/1.0/embed?url=http%3A%2F%2Fdomain.com%2F&quot; /&gt;
// &lt;link rel=&quot;https://api.w.org/&quot; href=&quot;http://domain.com/wp-json/&quot; /&gt;
// &lt;link rel=&quot;alternate&quot; type=&quot;application/json&quot; href=&quot;http://domain.com/wp-json/wp/v2/pages/345&quot; /&gt;
function remove_bloat() {
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'wp_oembed_add_discovery_links');

	// Disable the REST API endpoint
	add_filter('rest_authentication_errors', '__return_true');
	// Remove the REST API &lt;link&gt; elements
	remove_action('wp_head', 'rest_output_link_wp_head');
}
add_action('init', 'remove_bloat', 30);

// Remove Gutenberg block editor:
// &lt;link rel='stylesheet' id='classic-theme-styles-css' href='http://domain.com/wp-includes/css/classic-themes.min.css?ver=6.2.2' type='text/css' media='all' /&gt;
// &lt;link rel='stylesheet' id='wp-block-library-css' href='http://domain.com/wp-includes/css/dist/block-library/style.min.css?ver=6.2.2' type='text/css' media='all' /&gt;
// &lt;style id='global-styles-inline-css' type='text/css'&gt; ... &lt;/style&gt;
function remove_gutenberg() {
	wp_dequeue_style('classic-theme-styles');
	wp_dequeue_style('wp-block-library');
	wp_dequeue_style('global-styles');
}
add_action('wp_enqueue_scripts', 'remove_gutenberg', 30);

// Remove default icons:
// &lt;link rel=&quot;icon&quot; href=&quot;http://domain.com/wp-content/uploads/2023/03/cropped-favicon-32x32.png&quot; sizes=&quot;32x32&quot; /&gt;
// &lt;link rel=&quot;icon&quot; href=&quot;http://domain.com/wp-content/uploads/2023/03/cropped-favicon-192x192.png&quot; sizes=&quot;192x192&quot; /&gt;
// &lt;link rel=&quot;apple-touch-icon&quot; href=&quot;http://domain.com/wp-content/uploads/2023/03/cropped-favicon-180x180.png&quot; /&gt;
// &lt;meta name=&quot;msapplication-TileImage&quot; content=&quot;http://domain.com/wp-content/uploads/2023/03/cropped-favicon-270x270.png&quot; /&gt;
function default_favicons() {
	add_filter('get_site_icon_url', '__return_false');
}
add_action('init', 'default_favicons', 30);

// Remove emojis:
function disable_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');	
	// remove_filter('the_content_feed', 'wp_staticize_emoji');
	// remove_filter('comment_text_rss', 'wp_staticize_emoji');	
	// remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
add_action('init', 'disable_emojis', 30);

// Remove svg elements after opening &lt;body&gt; tag:
function remove_svg() {
	remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles');
	remove_action('wp_body_open', 'wp_global_styles_render_svg_filters');
}
add_action('init', 'remove_svg', 30)
?&gt;</code></pre>