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

<?php 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:

<?php
 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.

Comments

clever boy :)

posted by lola on 12.09.09 at 12:35 pm

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.

posted by Jesse on 05.07.10 at 6:04 am

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.

posted by mog on 05.07.10 at 10:01 am

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

posted by Danish Backer on 05.10.10 at 9:59 am

[...] 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: [...]

posted by Bruce on 05.19.10 at 4:21 pm

This is what I need. Thanks a lot!

posted by Derk on 05.27.10 at 1:06 pm

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

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

posted by Morten Skogly on 06.07.10 at 8:40 pm

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

posted by mog on 06.08.10 at 8:53 am

Hey this is great but I dont know what I am doing wrong or maybe it doesn’t work with the latest version of wordpress and latest CDN from jQuery Tools, but my page comes up completely blank when I try this method. Any thoughts?

posted by Duane on 10.13.10 at 10:40 pm

do you have a url ? :)

posted by mog on 10.13.10 at 10:53 pm

the url http://whatsmytip.com/ – is a work in progress. I did a little hack but its not ideal. I would love to get your method working.

posted by Duane on 10.13.10 at 11:16 pm

I am going to sleep now, ..but email me your header.php and function.php file from your template, zipped, to mog@mogmachine.com and i will look at it in the morning and tell you why.

posted by mog on 10.14.10 at 12:07 am

Hey I think I got a fix, thanks for the heads up. :)

posted by Duane on 10.14.10 at 12:43 am

Glad to hear it, ..let me know if the fix isn’t as much of a fix as you thought ;)

posted by mog on 10.14.10 at 10:57 am

I didn’t need to include the CDN version, just remove jQuery all together which I’ve never needed to do before so you just saved me a bunch of time – thanks!

posted by Gareth on 01.14.11 at 6:23 pm

you are the man,

thank you :)

posted by sinan işler on 01.28.11 at 1:09 am

you are the man.
i was trying to trick wp head, but the idea, simply exluding the call for jquery via the function.php never came to my mind.

thanks for making me a happy boy again.

posted by darth on 01.30.11 at 4:58 pm

Any idea why this would cause the WordPress Menu’s to not work? You can’t drag and drop the list items in the Menu’s section of WordPress. Remove the code, and it works again. Any ideas?

posted by paul on 03.09.11 at 6:43 am

Can you let me know the url, ..and if possible set me up some admin login details so I can take a look.
Email me at mog@mogmachine.com
Thanks,

posted by mog on 03.09.11 at 10:33 am

Excellent solution, and the code feels right – Thank you! :)

posted by Jonathan Wold on 03.17.11 at 8:15 pm

I load all my JS in the head (because I need to). How do I ensure I have a fallback for a CDN in the header?

I had something like this (see below) which worked until I moved it into functions.php with enqueue stuff.

if (!window.jQuery) {
document.write(unescape("%3Cscript src='/tpl/js/jquery/jquery-1.6.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}

posted by Dan on 07.21.11 at 11:09 am

I am a little confused, and when looking the first thing I came across was a duplicate post of this on the WP forum: http://wordpress.org/support/topic/jquery-from-google-cdn-fall-back-to-wp-local :) ..but I would load my first choice with my method using wp_head (functions.php) as I have stated, then use the fallback call immediately after this just in case, ..but in the header.php underneath wp_head() as done in the first post of the link above, ….instead using your cdn version instead of the loca one referenced.

posted by mog on 07.21.11 at 11:55 am

Indeed! You are a clever boy, thanks mate, this helped so much!

posted by Phill on 08.20.11 at 8:16 am

Leave a comment

Subscribe Scroll to Top