Clean up and optimize WordPress head tag

A mess in Wordpress head tag?

Dicembre 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

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.