A Better Way to Add Google CDN Hosted jQuery to WordPress

So there’s a few solutions out there to force WordPress to load jQuery from Google’s CDN, but fallback to local hosting in case the CDN is not available. Here’s what I’ve found to be the ideal solution. Add this code to your functions.php or even better, a custom plugin.

2 comments

Please — don’t do this! If you must, please use the correct version for your installation of WordPress, which you can find out by querying $wp_scripts.

Better still, use Jason Penney’s Use Google Libraries plugin that does what you’re doing, but also does the version check. It also handles some other scripts that are hosted on Google’s CDN, again using the appropriate version for your WordPress installation.

Sorry if this sounds harsh, but whilst I don’t mind the money, I don’t enjoy having to fix websites where someone has replaced the WordPress-bundled version of jQuery with some arbitrary “current at the time” version that breaks all manner of features after WordPress or some plugins are updated. This has become depressingly common.