{ Clean WP Head }

When looking at the raw source (or using a dev tool) it’s not easy to miss all of the “junk” that is added by default, especially in the <head> of the document. Even before you start adding plugins, there’s too much cruft. It’s also easy to go a bit overboard and start removing and dequeuing assets that you may actually need later, so I created a class that handles a minimal amount of cruft removal. I’ll be adding to it occasionally, or possibly create a plugin.

Inspiration for this came from how the Bones framework handles it – kudos.

Here’s the class.

I have this class in a namespace scope, but I removed it here for ease of use in other themes without namespaces. Note that this class should be called in an init hook, so basically:

functions.php

add_action( 'init', function() {
  new SiteLoad();
});

Without a namespace, I would also wrap the class in a class_exists() call as well:

if ( !( class_exists( 'SiteLoad' ) ) ) {
  class SiteLoad {
    ...
  }
}

I made the trimWpHead() method static so I need not instantiate the entire class. Given the right scope (WP_Hook init action) I can just use the scope resolution operator and call it statically.

SiteLoad::trimWpHead();

So if I have some additional functionality to add for various edge cases I can split the one method into separate ones and call them on a case by case basis.

SiteLoad::trimPluginCruft();
SiteLoad::trimOtherStuff();

Or even better:

lib\Hooks\Clean\SiteLoad::trimWpHead( $jquery = FALSE );
... etc.