I’m doing it right now with the following code:
function uw_load_scripts() {
// De-register the built in jQuery
wp_deregister_script('jquery');
// Register the CDN version
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), null, false);
// Load it in your theme
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'uw_load_scripts' );
This works, but should I do this for everyone, like this, or for everyone but admin (so that backend uses the WordPress version?):
if (function_exists('load_my_scripts')) {
function load_my_scripts() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', array(), null, false);
wp_enqueue_script('jquery');
}
}
}
add_action('init', 'load_my_scripts');
This version doesn’t work at all actually, I get the WordPress jQuery-version and not the Google one.
Therefore, should I deregister the jQuery that is included in WordPress at all?
Also, how do I add my own scripts (slider scripts, modernizr and my own custom.js) the correct way? I guess I should do this via functions.php as well and not in the header like I’m doing it now, but I’m unsure of how I would do that.
First rule of thumb: do not deregister core-bundled scripts and replace with other versions, unless you are absolutely certain that no Theme, Plugins, or core itself will break due to the version change. Really, unless you absolutely need an alternate version of a core-bundled script, just use what is bundled with core.
Second, I strongly recommend hooking into
wp_enqueue_scripts
for script registering and enqueueing, rather thaninit
. (It works atinit
, but from a play-nicely-with-others perspective, it’s best to use the most semantically correct hook.)Third, to enqueue your own custom scripts, you use the same methods as above:
Just add whatever scripts you need to enqueue.
Hope this helps, look up the codex for
wp_enqueue_scripts
for more information.init
to enqueue. Usewp_enqueue_scripts
for front-end stuff andadmin_enqueue_scripts
for admin side. You can useinit
to register scripts though.wp_enqueue_scripts
only fires on the front-end (and not on thelog-in page) – so you don’t have to check
is_admin()
.Unless you have a specific reason to do otherwise, I would suggest registering and queuing scripts using
functions.php
for themes or in a plug-in otherwise. You simply put:If the aim is to enqueue a script when a shortcode is used, you may wish to use
wp_enqueue_script
in the shortcode callback to queue it only when needed (this will print it in the footer since 3.3).You shouldn’t re-register the existing jQuery on the admin side. You may break something :D.
Plug-ins should not re-register the existing jQuery.
You should weigh up the pros and cons of re-registering jQuery. For instance it may break some plug-ins if you register an old version (maybe not now, but in the future…)
Fair warning: deregistering WP’s packaged version of jQuery in favor of your own can cause problems, especially if you aren’t extra careful to make sure that you change the version you’re pointing toward whenever WP updates its version. This goes doubly for plugins, which often (or often should, at least) write their plugins for maximum compatibility with the WP version of jQuery.
That said, your first version is correct – it’s hooked to
wp_enqueue_scripts
. Your second function is hooked toinit
, which may be why it’s not working properly.Add your own scripts in a similar manner:
I’m assuming here that you are loading scripts from a
js
directory in your current theme directory; change the URI parameter if that’s not true. The third parameterarray( 'jquery' )
says thatbbg-scripts
depends onjquery
, and so should be loaded afterward. See https://codex.wordpress.org/Function_Reference/wp_enqueue_script for more details.This isn’t going to do anything… I suspect you mean
Your example will only load the function load_my_scripts if it already exists (which it doesn’t so it won’t and if it did it would create an error)
If, for performance reasons, you want to load jquery and other core js-files from a CDN, make sure you are loading the same version to prevent nasty things happening with core and plugin functions. Like this:
After checking all different methods for loading jquery (not only on this post), I realized that none of them do all of these:
There a lot of alternate versions doing some of these in the list, but not all, so I wrote my version combing and modifing some of the methods already available. Here it is:
In order to save bandwidth and not ping Google every time the page is reloaded it remembers whether Google CDN is online or not for 5 minutes using the WordPress Transient API.