wp_head() remove redundant scripts?

There’s no filter that covers the entire output produced during wp_head(). You would have to use a fairly complicated process of output buffering starting before wp_head, then filtering out what you don’t want afterwards, before releasing the buffer.

Lets assume that you’re dealing with plugins that have registered their scripts properly> Try adding this to your functions.php and then viewing source of one of your pages:

add_action('wp_head', 'debug_scripts_queued');

function debug_scripts_queued() {
    global $wp_scripts;
    echo '<!--- SCRIPTS QUEUED'."\r\n";
    foreach ( $wp_scripts->queue as $script ) {
        echo "\r\nSCRIPT: ".$script."\r\n";
        $deps = $wp_scripts->registered[$script]->deps;
        if ($deps) {
            echo "DEPENDENCIES: ";
            print_r($deps);
        }
    }
    echo "\r\n--->";
}

That will list all the scripts actually registered, and their dependencies.

If what you want to deregister is in that list, and not being called as a dependency of another script, the easiest thing to do is just call wp_deregister_script with the handle listed here.

Most likely, though, you’re dealing with a case where a plugin didn’t follow best practices in adding a script. What it looks like from your output is that some plugin added jQuery 1.5 without first deregistering jquery and reregistering it with the different version. so anything that depends on jquery is forcing an include of the version bundled in WP.

Leave a Comment