Using Google-Hosted JavaScript Libraries with WordPress
by Simon. Average Reading Time: about a minute.
With the announcement that Google will be including page loading times as part of it’s SERPs ranking algorithm, it has become increasingly important to optimise your use of 3rd-party libraries such as jQuery.
If you want — and this is the default setting for WordPress, themes and plugins — you can just download jQuery, put it on your server and link to it from your header.php file in the <head> section.
However, it is better to use the proper wp_register_script() function, which can be achieved in your functions.php file:
if( !is_admin() ) { wp_deregister_script('jquery'); wp_register_script( 'jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false, '1.4.2' ); wp_enqueue_script('jquery'); }
The reason we use the wp_register_script() is to ensure that WordPress and its themes and plugins are aware that the script has been loaded and therefore not to load an additional copy.
I have also used the is_admin() function to prevent conflict and therefore errors, in the WordPress administrator.
Of course, this method is not only restricted to jQuery, you can do the same for other popular JavaScript frameworks such as Mootools.
Further information on the wp_enqueue_script can be found on the WordPress Codex.
