Google Hosted jQuery WordPress Function

By default, WordPress uses a locally hosted jQuery file, but you may not like this and instead, you may want to use Google hosted jQuery library – for speed and or reliability reasons. So, how do you do this? You can do this by adding a function to your theme’s functions.php file.

// Hosted jQuery 
if( !is_admin()){ wp_deregister_script('jquery'); wp_register_script('jquery', ("//"), false, null); wp_enqueue_script('jquery'); }

In the above example, Google’s hosted jQuery file will be provided to everyone who is not the WordPress admin and will be placed in the header.

The HTML output would look like this:


You can customize the output by modifying line four (4) of the function (first code example). Let’s take a look:

wp_register_script('jquery', ("//"), false, null);

This is what usage looks like:

wp_register_script( $handle, $src, $deps, $ver, $in_footer );

wpregisterscript is a WordPress function in of itself that registers a script. Then we defined the $handle as 'jquery'. Afterwards, we defined the source to be used or $src. We are not going to use any dependencies, so we set it to false. Take we look at $ver which can be a number or null. If you want the number, place the version number around single quotes. So in the example, the script is version 1.10.2. To define the version in the version, replace null as '1.10.2'. The last thing is to define the place where we want the script to be located, either the header or footer. For the header, do not define anything, like we did. However, if you want the script in the footer, replace $in_footer to true.

Ta-da! You just created an awesome WordPress function for a Google hosted jQuery file.