Tips & Tutorial

How to Load WordPress jQuery from Google CDN?

Oleh takien

By default, WordPress would load jQuery script from it’s own wp-includes directory. But, if you want, you can change it to jQuery which hosted by Google CDN. Why you should do this? It will load faster, because Google has server with better performance, and it might has been loaded by your visitor already. Yeah, they may have visited another website with the same jQuery URL. That’s mean the script already cached on their browser.

So, how to do that? Place this script somewhere in your functions.php of your theme:

function use_jquery_cdn() {
	if (!is_admin()) {
		wp_deregister_script('jquery');
		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', Array(), '1.11.3', true);
		wp_enqueue_script('jquery');
	}
}
add_action('init', 'use_jquery_cdn');

Tulisan ini sudah berumur: 10 tahun 6 bulan 30 hari. Informasi di dalamnya mungkin sudah tidak relevan, tapi nilai historisnya sangat berharga, terutama bagi saya sebagai penulisnya.

Tag Terkait: