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

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

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 Gutenberg notes

Collection of notes and thoughts on Wordpress Gutenberg blocks development.

January 9, 2023

Test page for Bright Link Preview plugin

To test the plugin hover your mouse to a link, or tap the link on mobile device.

December 4, 2022

Updating a metabox after Gutenberg updates

When press on "Update" refresh a custom metabox under gutenberg editor.