Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

  1. 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.
  2. User then visits my site which loads ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js?ver=3.0.4
  3. 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/

share|improve this question
add comment

6 Answers

up vote 1 down vote accepted

Use null as $ver param:

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, null);
wp_enqueue_script('jquery');

Output:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
share|improve this answer
    
Good catch! This seems to be recent improvement, I remember in the past null was treated like false and ignored, effectively leaving default version applied. –  Rarst Feb 9 '11 at 19:01
    
Nicely done @lemats - you get the tick. I'm pretty happy the WP team have made this change. –  Eoin Kelly Sep 4 '11 at 23:46
add comment

I remove version like this, can be easily written out to match multiple domains:

add_filter( 'script_loader_src', 'jquery_unversion' );

function jquery_unversion( $src ) {

    if( strpos( $src, 'ajax.googleapis.com' ) )
        $src = remove_query_arg( 'ver', $src );

    return $src;
}
share|improve this answer
add comment

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 of wp_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.)

share|improve this answer
    
Thanks Peter. Do you think it's worth raising as an issue on traq? –  Eoin Kelly Jan 26 '11 at 4:33
1  
I'm afraid I'm a bit cynical about the WP core group's interest in fixing things like this. And that's after only about 6 months of dealing with WP. To answer your question, it couldn't hurt. I'm sure they would want the current behavior to be the default, but it's clearly wrong to tack this on to everything you want queued for loading. –  Peter Rowell Jan 26 '11 at 4:52
    
Thanks for your time & replies Peter –  Eoin Kelly Jan 26 '11 at 4:54
add comment

Use this in your theme's functions.php

function remove_wp_script_version( $src ){

    global $wp_version;

  $version_str = '?ver='.$wp_version;
  $version_str_offset = strlen( $src ) - strlen( $version_str );

  if( substr( $src, $version_str_offset ) == $version_str )
    return substr( $src, 0, $version_str_offset );
  else
    return $src;
}
add_filter( 'script_loader_src', 'remove_wp_script_version', 15, 1 ); 

If you want, you can also add another filter to remove versioning from stylesheets.

add_filter( 'style_loader_src', 'remove_script_version', 15, 1 );

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.

share|improve this answer
    
TIP: You don't need the 1 in your actions, 1 is default. –  t31os Jan 26 '11 at 14:00
add comment

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.

share|improve this answer
    
Nice plugin, but it also good to know how to do it yourself for files that are not hosted by Google (for example I think Microsoft hosts jQuery Cycle, but Google doesn't). –  Rarst Jan 27 '11 at 6:49
    
Cool plugin. Thanks for the tip. –  Eoin Kelly Jan 28 '11 at 3:47
add comment

Thank you all for your responses. In the end this is the code I used (posting here for the sake of future searchers):

function control_wp_url_versioning($src)
{
    // $src is the URL that WP has generated for the script or stlye you added 
    // with wp_enqueue_script() or wp_enqueue_style(). This function currently 
    // removes the version string off *all* scripts. If you need to do something 
    // different, then you should do it here.
    $src = remove_query_arg( 'ver', $src );
    return $src;
}

// The default script priority is 10. We load these filters with priority 15 to 
// ensure they are run *after* all the default filters have run. 
add_filter('script_loader_src', 'control_wp_url_versioning', 15); 
add_filter('style_loader_src', 'control_wp_url_versioning', 15); 
share|improve this answer
    
Note that sometimes you do want version tags, if for example timestamp version is used on own files so that it skips caching when file changes. So blanket removal of versions will be fine most of the time, just don't forget that you have done that when corner cases matter. –  Rarst Jan 27 '11 at 6:52
    
Agreed. I am using WP-Minify which adds it's own cache-bursting timestamp so a blanket removal works well for me. –  Eoin Kelly Jan 28 '11 at 3:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.