Everyone is installing jQuery library on my site

I am using WordPress, it comes with a built-in jQuery library. When I install a plugin, another version gets its way in. I learn to write my own code, the tutorial asks me to install another one!

At the time of this writing, I probably have 4-5 versions of jQuery library installed!

Read More

It’s going crazy! and I don’t think I need that many, I just need the latest one from google CDN for best performance, so I put this line in my php file, but it didn’t seem to show up anywhere even after I cleared the cache.

function mytheme_jquery_enqueue(){
    if(!is_admin()){
        wp_deregister_script('jquery');
        wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js", false, null);
        wp_enqueue_script('jquery');  
    }
}
add_action('wp_enqueue_scripts', 'mytheme_jquery_enqueue');   

How do I fix it and get rid of every other unnecessary version?

Related posts

Leave a Reply

1 comment

  1. First, try to give your hook a lower priority, e.g.:

    add_action( 'wp_enqueue_scripts', 'mytheme_jquery_enqueue', 99999 ); 
    

    This way, it’ll be the latest to run, unless some plugin has a priority of 9999999it’s a horse race


    Then, Enqueuing jQuery in plug-ins

    if your code (or the theme’s code) is unregistering jQuery and
    re-registering it from another location (Google), you need to add this
    somewhere:

    jQuery.noConflict(); 
    

    This makes jQuery cooperate with other scripts
    (i.e. Prototype) that try to define the $ variable globally. WordPress
    has this line bundled into their version, Google does not.


    But, better than enqueuing the CDN yourself, it’s recommended to use this plugin: Use Google Libraries. According to a core developer:

    Please do not use that method to include the jQuery script from Google. The above doesn’t work and will cause conflicts with other scripts, along with other issues. Use the “Use Google Libraries” plugin instead, if you want to use Google hosted versions of the libraries. This plugin does it right, and is updated often. The method above will only work superficially, it will cause problems down the line. – Otto Jul 29 ’12 at 15:57


    Then, we have the issue that it is really not recommended to load other jQuery libraries and use the one bundled with WordPress. In which case, you should use remove_action, wp_deregister_script or wp_dequeue_script to prevent all those plugins or theme to load their versions.

    Don’t Dequeue WordPress’ jQuery

    As a moderator on the WordPress Stack Exchange, I end up spending a
    lot of time on the site. I see lots of great questions, lots of
    not-so-great questions, and several you’ve-got-to-be-kidding-me
    questions. But the question I see the most often frustrates me:

    How do I remove WordPress’ jQuery and use Google’s CDN version instead?

    I have no trouble saying that, if you’re asking this question, you
    have no business building a website in the first place.