08 Dec 2009

Stop WordPress loading JQuery in wp_head()

wordpress-plugin-2A problem i have been encountering a lot recently especailly through my extensive use of JQuery Tools is being able to stop wordpress loading JQuery in in wp_head().

I simply wanted to be able to load JQuery with the tools UI library included from the rather useful CDN they support by inserting the following into my header.php file.

<script src=”http://cdn.jquerytools.org/1.1.2/jquery.tools.min.js”></script>

However all was not so simple, as plugins would still overide this and try and load in the latest version at least once.

WordPress was not recognising this as a valid version, even when using the

wp_enqueue_script(‘jquery’);

technique which seems to be fine for loading a standard version in wp_head, and is employed by most if not all good plugins to ensure you are not loading multiple instnaces of the same version.

What i wanted to do was stop JQuery loading altogether, essentially tell wp_head that everything was cool I would be taking care of jQuery.

WARNING: This is intended for those loading jquery from either the google code cdn, or a similar cdn that promises to deliver the latest version. Version upgrades by these distribution networks might in the future render certain plugins broken, ..(or at least needing some love).
In my case as stated earlier I am using the JQuery Tools library.

The solution, ..
Drop this into your functions.php file in yoru active theme:

if( !is_admin()){
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, (“http://cdn.jquerytools.org/1.1.2/jquery.tools.min.js”), false, ‘1.3.2′);
//wp_register_script(‘jquery’, (“http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”), false, ‘1.3.2′);
wp_enqueue_script(‘jquery’);
}

You can see that i have left a commented out line for using the google api version, you can easily comment out my tools line an uncomment the google one if you wish.
Now none of your plugins will load in their verison of JQuery (providing they have been well coded) as WP tells them it is already there.

Problem solved.

11 Comments so far

  1. lola says:

    clever boy :)

  2. Wordpress-Performance: jquery von extern einbinden : powerbook_blog says:

    [...] mit dem Hamburger darüber gestolpert, dass Wordpress teils jquery über wp_head() einbindet. Bei Mog Machine gibt es eine Anleitung für einen eleganten Workaround [...]

  3. Dobschat » Links vom 27. Januar 2010 bis 30. Januar 2010 says:

    [...] Januar 2010 bei delicious gespeicherte Links:How to make The Internet (from The IT Crowd). –Stop WordPress loading JQuery in wp_head() | Mog Machine –Central Atomics | Rocketbox – Powerful e-mail search for Apple Mail –Mac: [...]

  4. Stop WordPress loading JQuery in wp_head() | WPLover says:

    [...] Stop WordPress loading JQuery in wp_head() [...]

  5. Jesse says:

    im having trouble with jquery tools and wordpress. are there any other gotchas i should be aware of? i can get the code working locally for the tooltip functionality but when i pull it into my theme it doesnt work. It appears as if the js isn’t being called.

  6. mog says:

    Do you have a url i can look at? …and how are you including the jquery tools library, their CDN, your CDN, or a manual overide in the functions.php ? ..or are you just putting the script call in the header?

    LEt me know and i will take a look for you.

  7. Danish Backer says:

    Thanks A lot man!. Thanks again for sharing your code.

  8. Bruce says:

    [...] Januar 2010 bei delicious gespeicherte Links:How to make The Internet (from The IT Crowd). –Stop WordPress loading JQuery in wp_head() | Mog Machine –Central Atomics | Rocketbox – Powerful e-mail search for Apple Mail –Mac: [...]

  9. Derk says:

    This is what I need. Thanks a lot!

  10. Morten Skogly says:

    Why is it wrapped in if( !is_admin()){ }

    Does that mean it only runs for a logged in admin?

  11. mog says:

    No, ..it includes for everyone, ..not to worry.

Leave a Comment

Inappropriate comments will be removed at admin's discretion.