Dynamic Web Lab
Back to insights

Load jquery library from google CDN in wordpress

December 18, 2012
Share insight

Add this code on wordpress theme functions.php to deregister the urgent jquery and load jquery library from google.

[php]
function load_cdn_jquery() {

// only use this method is we’re not in wp-admin
if (!is_admin()) {

// deregister the original version of jQuery
wp_deregister_script(‘jquery’);

// discover the correct protocol to use
$protocol=’http:’;
if($_SERVER[‘HTTPS’]==’on’) {
$protocol=’https:’;
}

// register the Google CDN version
wp_register_script(‘jquery’, $protocol.’//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js’, false, ‘1.5.2’);

// add it back into the queue
wp_enqueue_script(‘jquery’);

}

}

add_action(‘template_redirect’, ‘load_cdn_jquery’);
[/php]