Cleaning up WordPress’ head

There are a lot of tags in the head that most of us don’t need or even know what to do with. I used to grab the code from 2 or 3 different sites to clean all of it up in my themes but now I’m compiling it all here.

If you’d like to save some head space then copy/paste this into the functions.php of your theme.

/**
 * Removes actions and filters to clean up the head
 */
function rational_head_clean() {
	// https://scotch.io/tutorials/removing-wordpress-header-junk
	remove_action( 'wp_head', 'rsd_link' );
	remove_action( 'wp_head', 'wp_generator' );
	remove_action( 'wp_head', 'feed_links', 2 );
	remove_action( 'wp_head', 'feed_links_extra', 3 );
	remove_action( 'wp_head', 'wlwmanifest_link' );
	remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
	remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
	remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
	
	// http://wordpress.stackexchange.com/a/185578/26817
	remove_action( 'admin_print_styles', 'print_emoji_styles' );
	remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
	remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
	remove_action( 'wp_print_styles', 'print_emoji_styles' );
	remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
	remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
	remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
	add_filter( 'emoji_svg_url', '__return_false' );
	add_filter( 'tiny_mce_plugins', 'rational_tiny_mce_plugins_clean' );
	
	// http://wordpress.stackexchange.com/a/211469/26817
	remove_action( 'wp_head', 'rest_output_link_wp_head' );
	remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
	remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
	
}
add_action( 'init', 'rational_head_clean' );

/**
 * Filtering TinyMCE plugins
 *
 * @param	array	$plugins	Array of TinyMCE plugins
 *
 * @return	array				Filtered array of TinyMCE plugins
 */
function rational_tiny_mce_plugins( $plugins ) {
	if ( is_array( $plugins ) )
		return array_diff( $plugins, array( 'wpemoji' ) );
	else
		return array();
}

Share

Comments