Dynamic Web Lab
Back to insights

Load jquery library from google CDN in wordpress

December 18, 20121 min read

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]

Writer

Dynamic Web Lab Editorial

We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.

Share this article

Tags

jqueryPHPwordpress

Need help implementing this?

We turn these playbooks into shipped features. Let us scope your roadmap and support your team.

Start a project conversation