0

quick question. I'm using jquery for my current project (the latest release). A few plugins I'm using are using jquery as well and they are inserted through wp_head(). Since I want my website to ber fairly slim and fast this redundant scripts should be removed.

is there any way I can filter for the string "jquery.js" in wp_head() and remove the tag that embeds the script? As you can see in the following screenshot jquery.js wouldn't be needed. Can I remove that with a hook?

enter image description here

3
  • You are dependant on the plugin authors properly registering the scripts. A lot of plugins simply throw their scripts into the head without using wp_register_script and the related APIs. Can you find which plugins are adding which script and how they're doing it? Commented Mar 28, 2011 at 19:53
  • is there some way to find out where it's coming from? it seems like none of my plugins is loading this script. Neither does my emebedded google map or the facebook button. However it's there. Any idea how I can see what is loading this script? Commented Mar 28, 2011 at 20:23
  • well sorry, it must come from a plugin, because when i get rid of wp_head() in my header it's gone. is there another way to add a filter to wp_head() and simply filter "script.js"? Commented Mar 28, 2011 at 20:28

3 Answers 3

3

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.

2

You can use wp_deregister_style() and wp_deregister_script() to clean up overly aggressive Plugin script/style injections. For example, here's how I "optimize" Contact Form 7's style/script injections:

// Optimize Contact Form 7
function deregister_cf7_js() {
   if ( ! is_page( 'contact' ) ) {
    wp_deregister_script( 'contact-form-7' );
     }
}
add_action( 'wp_print_scripts', 'deregister_cf7_js', 100 );

function deregister_ct7_styles() {
   if ( ! is_page( 'contact' ) ) {
        wp_deregister_style( 'contact-form-7' );
    }
}
add_action( 'wp_print_styles', 'deregister_ct7_styles', 100 );

If you know which Plugins are the culprits, you can use a similar approach.

0

Yes, you can disable specific plugins or scripts form appearing.

Here's an example how I disabled the easy fancybox plugin from appearing

Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.