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!
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?
First, try to give your hook a lower priority, e.g.:
This way, it’ll be the latest to run, unless some plugin has a priority of
9999999
… it’s a horse race…Then, Enqueuing jQuery in plug-ins
But, better than enqueuing the CDN yourself, it’s recommended to use this plugin: Use Google Libraries. According to a core developer:
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
orwp_dequeue_script
to prevent all those plugins or theme to load their versions.