Forums

Enqueue/register script - remove version # ? (6 posts)

  1. misstee
    Member
    Posted 11 months ago #

    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.

  2. raskull
    Member
    Posted 2 months ago #

    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

  3. Kawauso
    Member
    Posted 2 months ago #

    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.

  4. raskull
    Member
    Posted 2 months ago #

    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?

  5. Kawauso
    Member
    Posted 2 months ago #

    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 :)

  6. Kawauso
    Member
    Posted 2 months ago #

    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 :)

Reply

You must log in to post.

About this Topic