How can I clean unnecessary code in WordPress wp_head file?

  

4
Topic starter

Hi, I would like to clean my code in my WordPress theme.

I know that in wp_head there are some functions which trigger lines of code - not very useful so they can be deleted;

Can you please share how can I clean my wp_head from unnecessary code and maybe share some additional cleaning WP tricks...

Thanks

2 Answers
3

First, you must know that if you don't use a selfmade theme or child theme the below shared changes will be overwritten with the next theme update!

To prevent this: - create a child theme + use the functions.php file there or make a small plugin out of the code below:

//custom functions:
remove_action('wp_head', 'rsd_link'); // Display the link to the Really Simple Discovery service endpoint, EditURI link
remove_action('wp_head', 'wlwmanifest_link'); // Display the link to the Windows Live Writer manifest file.
remove_action('wp_head', 'wp_generator'); // Display the XHTML generator that is generated on the wp_head hook, WP version
remove_action('wp_head', 'print_emoji_detection_script', 7); //Removes the emoji code
remove_action('wp_print_styles', 'print_emoji_styles'); //Removes the emoji code 2
remove_action('wp_head', 'feed_links_extra', 3); // Display the links to the extra feeds such as category feeds
remove_action('wp_head', 'feed_links', 2); // Display the links to the general feeds: Post and Comment Feed
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // prev link
remove_action('wp_head', 'start_post_rel_link', 10, 0); // start link
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // Display relational links for the posts adjacent to the current post.
remove_action('wp_head', 'index_rel_link'); // index link
 
// Remove pingback link  
if (!is_admin()) {
    function link_rel_buffer_callback($buffer) {
        $buffer = preg_replace('/(<link.*?rel=("|\')pingback("|\').*?href=("|\')(.*?)("|\')(.*?)?\/?>|<link.*?href=("|\')(.*?)("|\').*?rel=("|\')pingback("|\')(.*?)?\/?>)/i', '', $buffer);
        return $buffer;
    }
 
    function link_rel_buffer_start() {
        ob_start("link_rel_buffer_callback");
    }
 
    function link_rel_buffer_end() {
        ob_flush();
    }
 
    add_action('template_redirect', 'link_rel_buffer_start', -1);
    add_action('get_header', 'link_rel_buffer_start');
    add_action('wp_head', 'link_rel_buffer_end', 999);
}

Please read the comments - every line of code is explained there.

2

You can also use: Perfmatters WP plugin

It is a simple, lightweight WordPress plugin built to speed up your site in a few easy steps! 🌟 Created by two brothers with years of experience, it’s designed to help anyone make their site load faster.

Key Features 🔑

  • Script Manager 📝 – Choose which scripts load on each page, cutting down on unnecessary stuff.
  • Database Cleanup 🧹 – Get rid of junk like revisions, spam comments, and expired data, keeping your site fast and tidy.
  • Lazy Loading 💤 – Delay images, videos, and iframes until they’re actually needed, improving initial load times.
  • CDN Support 🌍 – Connect to a Content Delivery Network to serve your content faster worldwide.
  • Google Analytics Tweak 📊 – Host Analytics locally to reduce outside requests and speed things up.

Perfmatters works smoothly with most themes and plugins and can be paired with caching plugins for even better results. Regular updates and top-notch support mean it’s always improving. 💪

For details, check out their official website

Share: