Cleaning Up WordPress Head

<h3 class="entry-title">Cleaning Up WordPress Head</h3>
Posted on



By default, WordPress will include a bunch of information in the <head> section of the site. Below is a snippet of actions I found while researching the topic that I include in the sites I work on in order to reduce the clutter and ultimately speed up the page load time.

<?php 


remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

function deregister_jquery() {  
    if ( !is_admin() ) {
        wp_deregister_script('jquery');
    }
}  

add_action('wp_enqueue_scripts', 'deregister_jquery'); 

function tcp_deregister_scripts(){
    wp_deregister_script( 'wp-embed' );
}
  add_action( 'wp_footer', 'tcp_deregister_scripts' );

You will also note that I am deregistering jQuery. This is because the default version included in WordPress presents a vulnerability according to Google's LightHouse Audit tool. I usually include a new version of jQuery on the frontend if it is needed.

Another aspect I like to cleanup is the Nav Menu classes and ID's. I don't often take advantage of theme and would prefer that my markup is a bit cleaner. A quick google search turned up the below code snippet that can be included in your theme's function.php

/**
 * @link http://www.wp3layouts.com/wp/remove-every-class-id-wordpress-wp_nav_menu/
 *  */ 
function remove_css_id_filter($var) {
    return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
} 
add_filter( 'page_css_class', 'remove_css_id_filter', 100, 1);
add_filter( 'nav_menu_item_id', 'remove_css_id_filter', 100, 1);
add_filter( 'nav_menu_css_class', 'remove_css_id_filter', 100, 1);