Everyone installs the jQuery library on my site

I am using Wordpress, it comes with jQuery library built in. When I install the plugin, another version is launched. I will 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 the jQuery library installed!

This is going crazy! and I don't think I need it, I just need the latest from google CDN for better performance, so I put this line in my php file, but it doesn'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 this and get rid of any other unnecessary version?

+3


source to share


1 answer


Try making your hook a lower priority first, like this:

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

      

So it will be the last one to run if some plugin has priority 9999999

... it's jumps ...


Then Running jQuery in plugins

if your code (or theme code) is not registering jQuery and re-registering from elsewhere (Google) you need to add this where:

jQuery.noConflict(); 

      

This forces jQuery to interact with other scripts (i.e. Prototype) that try to define the $ variable globally. WordPress this line is included in their version, Google doesn't.




But, better than forcing the CDN yourself, it is recommended to use this plugin: Use Google Libraries . According to the main developer:

Please do not use this method to include jQuery script from Google. The above does not work and will cause conflicts with other scripts as well as other problems. Instead, use the Use Google Libraries plugin if you want to use Google hosted versions of the libraries. This plugin does it right and is updated frequently. The above method will only work superficially, it will cause problems on the line. - Otto Jun 29 @ 12:57 pm


Then we have the problem that it is actually not recommended to load other jQuery libraries and use the one bundled with WordPress. In this case, you should use remove_action

, wp_deregister_script

or wp_dequeue_script

, to prevent loading all these plugins or themes.

Dont Dequeue WordPress jQuery

As a moderator at the WordPress Stack Exchange , I end up spending a lot of time on the site. I see a lot of big questions, a lot of not-so-big questions, and several of you have learned-let's-joke-me questions. But the question I see most often disappoints me:

How do I uninstall WordPress jQuery and use the CDN version for Googles?

I have nothing to say, if you are asking this question, you are not primarily building a website.

+2


source







All Articles