Blog / WordPress/ WordPress Optimization: Remove Extra Head Tags (JS, CSS, Feed, Version)

WordPress Optimization: Remove Extra Head Tags (JS, CSS, Feed, Version)

For WordPress users focused on performance and clean code, many resources loaded by default in the page head (such as the WordPress version number, extra RSS Feed links, DNS prefetching, etc.) may not be necessary and could pose minor security or performance concerns.

By adding a few lines of simple code, you can clean up these extra items, resulting in cleaner HTML source. Below is a complete, corrected code snippet suitable for current mainstream WordPress versions.

Clean Up Head Code

Add the following code to the end of your active theme's functions.php file.

/**
 * Clean up extra items in WordPress head
 */
function wukongsou_cleanup_wp_head() {
    // Remove WordPress version
    remove_action('wp_head', 'wp_generator');
    // Remove RSD and WLW links
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    // Remove adjacent post links
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
    // Remove feed links
    remove_action('wp_head', 'feed_links', 2);
    remove_action('wp_head', 'feed_links_extra', 3);
    // Remove canonical link
    remove_action('wp_head', 'rel_canonical');
    // Remove shortlink
    remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
    // Remove Emoji scripts/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_action('admin_print_styles', 'print_emoji_styles');
    // Remove REST API and oEmbed links
    remove_action('wp_head', 'rest_output_link_wp_head', 10);
    remove_action('wp_head', 'wp_oembed_add_discovery_links');
    remove_action('wp_head', 'wp_oembed_add_host_js');
    // Remove DNS prefetch hints
    remove_action('wp_head', 'wp_resource_hints', 2);
    // Hide admin bar on frontend
    add_filter('show_admin_bar', '__return_false');
}
add_action('init', 'wukongsou_cleanup_wp_head');

Explanation & Notes

  • Version Number: wp_generator outputs the WordPress version; removing it can slightly improve security.
  • Feed Links: Remove these if your site does not use RSS/Atom feeds.
  • Emoji Support: Removing this stops WordPress from loading CSS/JS for emoji. Keep if you need emoji in posts/comments.
  • REST API Links: Remove if your theme/plugins do not use the WordPress REST API.
  • Testing: After saving functions.php, refresh your site and use browser developer tools (F12) to view the page source and verify the head section is cleaner.

Further Optimization Tips

  • Use performance plugins (e.g., Autoptimize, WP Rocket) to combine and minify CSS/JS.
  • Consider a CDN for static assets like images and fonts.
  • Regularly clean your database and post revisions to reduce unnecessary queries.

These steps help streamline WordPress head output, making code cleaner and potentially improving site load speed.

Post a Comment

Your email will not be published. Required fields are marked with *.