• With wp_register_script, is it possible to remove (omit) the version number from the output? I’ve tried false, empty quotation marks, 0, and just leaving out the variable altogether and the script output still inherits WP’s version, 2.7.

Viewing 5 replies - 1 through 5 (of 5 total)
  • I need to know this too!

    following this model:
    wp_enqueue_style( $handle, $src, $deps, $ver, $media )

    yields this:

    wp_enqueue_style( 'mystyle', get_template_directory_uri() . '/mystyle.css', false, false,'all' );

    which prints this no matter what:

    <link rel='stylesheet' id='mystyle-css' href='http://myplace.com/wp-content/themes/mytheme/mystyle.css?ver=2.8.4' type='text/css' media='all' />

    $ver as false prints ?ver=2.8.4
    $ver as ” prints ?ver=2.8.4
    $ver as ‘foo’ prints ?ver=foo
    hell, even $ver as foo (no quotes) prints ?ver=foo

    The version number is included as part of the function so that if the script or stylesheet is updated, the URL changes and caching mechanisms won’t serve an old version.

    That said, after having just poked around the deepest, darkest depths of the script loader (and boy is that thing ridiculously complicated), it’s perfectly possible to take out with a couple of filters.

    <?
    /*
    Plugin name: Strip WP Version in Stylesheets/Scripts
    */
    
    add_filter( 'script_loader_src', 'remove_src_version' );
    add_filter( 'style_loader_src', 'remove_src_version' );
    
    function remove_src_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;
    
    }
    
    ?>

    That was acting up for me, but I’m pretty sure that’s because I’m on a 2.9 dev build that does funky things with the $wp_version… like not having one. That should strip out the WordPress version from any stylesheets and scripts.

    Wow, I haven’t had a chance to study this, and I never would have figured it out myself, but that totally worked! Thanks!

    Still the rhetorical question: isn’t $ver = false *supposed* to do away with a version appendage?

    It would make sense, but as far as the code is concerned that’s not a string, so it happily defaults to what it’s been told 🙂

    Whoops. If anyone else reads this, that should open with <?php rather than <? or it won’t work on some setups. You can take out the else too, but that’s besides the point 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Enqueue/register script – remove version # ?’ is closed to new replies.