Clean up and optimize WordPress head tag

If you need to know how to remove RSS Feed URL’s from page head or how to remove post relational…

December 11, 2015

If you need to know how to remove RSS Feed URL’s from page head or how to remove post relational links from wp_head read this article! The way to modify the content in head tag is to use remove_action function and add_filter function to hook the original WordPress functions and modify the html.
You have to add these code, in your functions.php file in your theme, to remove many lines of your head tag that often are not useful for your theme:

function clean_wp_head () {
   remove_action('wp_head', 'wp_generator');
   remove_action('wp_head', 'wlwmanifest_link');
   remove_action('wp_head', 'rsd_link');
   remove_action('wp_head', 'wp_shortlink_wp_head');
   remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
   remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
   remove_action( 'wp_print_styles', 'print_emoji_styles' );
   add_filter('the_generator', '__return_false');
   add_filter('show_admin_bar','__return_false');
}
add_action('after_setup_theme', 'clean_wp_head');

Modify relational links (adjacent_posts_rel_link_wp_head) when SEO Yoast plugin is installed

Sometimes these trick doesn’t remove all the lines. For example, if you have installed the WordPress SEO plugin (YOAST), the above code will not work for the adjacent_posts_rel_link_wp_head and you will still see the rel="next" in the link:

<link rel="next" href="http://www.barattalo.it/page/2/" />

Add these lines to remove the relational post links in your html head if you have Seo by Yoast Plugin installed:

$wpseo_front = WPSEO_Frontend::get_instance();
remove_action('wpseo_head', array( $wpseo_front, 'adjacent_rel_links' ), 21 );

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Modify the language attribute based on category in WordPress

How to modify the language attribute in your Wordpress theme using a specific value

November 7, 2019

Add filter on wp_title not working with Yoast SEO plugin

For SEO purposes, in a specific template that has a list of items with pagination, I need to have different…

February 22, 2017

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019

WordPress Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

How I've beat the php mysterious fatal error unknown in Wordpress caused by caching plugin WP Fastest Cache

November 7, 2019