Replace Default WordPress jQuery Script with Google Library

By default, WordPress comes bundled with a bunch of libraries. These Libraries can be used while developing WordPress themes and plugins. One of the most common library used by developers is jQuery. In this post, we will see how we can replace the default jQuery library in WordPress with the one hosted on Google.

Why you should replace the default WordPress jQuery Library with the one hosted on Google?

Google hosted library is being used on a thousands of websites. Chances are your current visitor has also visited one of these websites. Hence, the visitor may not need to download jQuery at all as the script is already cached in his/her web browser making it load much faster.

To replace you self-hosted jQuery library with one hosted on Google, copy the following code and paste it inside your functions.php file located within your theme directory.

function replace_jquery() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', false, '1.11.1');
wp_enqueue_script('jquery');
}
}
add_action('init', 'replace_jquery');