I am loading JQuery from the google CDN using the following code:
wp_deregister_script('jquery');
wp_register_script(
'jquery', // handle - WP uses this name to refer to script
'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
array(), // Array of handles representing scripts that this one depends on.
false, // Version number - defaults to false.
false // false = put script in header, true = put in footer
);
wp_enqueue_script('jquery');
In firebug I see that wordpress appends ‘?ver=3.0.4’ to the URL to control caching. In fact there doesn’t seem to be any way to stop WP from appending something to the URL – I can provide my own string in the call to wp_register_script() but WP will use the default ‘ver=3.0.4’ if i leave it blank (or “false”)
I believe the appended version string stop the user’s browser from re-using a cached copy of the file that it might have downloaded from a different website. E.g.
- User visits www.example.com which loads ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js so it is now in browser cache.
- User then visits my site which loads ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js?ver=3.0.4
- The user’s browser not use it’s cached copy because the URLs are different.
Since cross-site caching is one of the main reasons I want to use Google’s CDN, are there any solutions to this other than loading the script manually (not ideal) or hacking WP core?
TIA
/Eoin/
Use null as $ver param:
Output:
I remove version like this, can be easily written out to match multiple domains:
Short answer: No.
I looked at the code in
wp-includes/scriptloader.php
and there is nothing that indicates an “option” about supplying the version number.You can, however, simply put this in your footer (or header) as an explicit
<script>
tag. Of course, this completely defeats the whole idea ofwp_register_script()
, but that’s true with a number of “features” in WP. (Don’t get me started on the brain-dead/broken parsing of shortcodes with regexps.)Use this in your theme’s functions.php
If you want, you can also add another filter to remove versioning from stylesheets.
I don’t understand why they put this in the core. Not only does it mess with caching of external scripts but it also gives out unnecessary information about the site. I just use the filters above and add my own version numbers to custom scripts as needed.
Including jQuery from Google’s CDN also requires some extra code to handle the noConflict mode issue.
I suggest that instead of rolling your own, use a plugin designed to handle it properly. This one works great:
http://wordpress.org/extend/plugins/use-google-libraries/
Simple no-hassle setup. Just activate it and it will replace all relevant libraries with Google’s CDN versions.
Thank you all for your responses. In the end this is the code I used (posting here for the sake of future searchers):