wordpress-heart-jquery
Stop WordPress loading JQuery in wp_head()

A 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.7.2/jquery.min.js"), false, '1.7.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.

Leave a comment...

32 comments

  1. lola says:

    clever boy :)

  2. 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.

  3. 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.

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

  5. Derk says:

    This is what I need. Thanks a lot!

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

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

  7. mog says:

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

  8. Duane says:

    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?

  9. mog says:

    do you have a url ? :)

  10. Duane says:

    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.

  11. mog says:

    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.

  12. Duane says:

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

  13. mog says:

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

  14. Gareth says:

    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!

  15. sinan işler says:

    you are the man,

    thank you :)

  16. darth says:

    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.

  17. paul says:

    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?

  18. mog says:

    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,

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

  20. Dan says:

    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"));
    }

  21. mog says:

    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.

  22. Phill says:

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

  23. Henry says:

    This shaved 2.2 seconds of my page load time. I had 4.6 seconds and now it is 2.4 seconds. Very nice tip. Thank you.

  24. mog says:

    Generally tidying up your header.php in WP will do that ….but you have a really nice clean source code actually, ..wuite a relief, ..although you can use your selection of these to tody it even more (in your functions.php file) …I would always remove the WP generator version for instance.

    remove_action( ‘wp_head’, ‘feed_links’ );
    remove_action( ‘wp_head’, ‘rsd_link’);
    remove_action( ‘wp_head’, ‘wlwmanifest_link’);
    remove_action( ‘wp_head’, ‘index_rel_link’);
    remove_action( ‘wp_head’, ‘parent_post_rel_link’);
    remove_action( ‘wp_head’, ‘start_post_rel_link’);
    remove_action( ‘wp_head’, ‘adjacent_posts_rel_link’);
    remove_action( ‘wp_head’, ‘wp_generator’);

    ..and then maybe change your jQuery to the latest, 1.7.1 ..not 1.3.1 ;)

  25. Paul says:

    $protocol=’http:’;
    if (!empty($_SERVER['HTTPS'])) {
    $protocol=’https:’;
    }
    wp_deregister_script(‘jquery’);
    wp_register_script(‘jquery’, $protocol.’//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js’);
    wp_enqueue_script(‘jquery’);

    this includes SSL support

  26. mog says:

    Nice, …thank you for adding that ….personally on a site with SSL I just made sure it was always being served from https:// but this is a handy couple of lines for multiple uses to serve files on https.

  27. David J Rush says:

    Thank you so much! I’ve been looking for a solution for this for ages that wouldn’t impact my plugins or get overwritten whenever WordPress updates. Awesome!

This site is not up to date. I really should take it down and make a new one as I have been busy with a lot of cool projects and clients recently, but currently do not have the time to rebuild it.

So, it stays here, but please note it has not been updated in a while and if you need to get in contact with me please just email me directly at mog@mogmachine.com or phone me on +44 (0)7960 214407.

Thanks,

Marcus (mog)