Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created June 16, 2015 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boonebgorges/1c46f06939b701a1ef43 to your computer and use it in GitHub Desktop.
Save boonebgorges/1c46f06939b701a1ef43 to your computer and use it in GitHub Desktop.
Custom version querystring appending for WordPress JS and CSS assets
<?php
/**
* Note that CAC_VERSION is a custom constant. Replace as necessary.
*/
function cac_asset_ver( $tag, $handle, $src = '' ) {
// 'style_loader_tag' doesn't pass a src, so we sniff it from the tag.
if ( ! $src ) {
preg_match( '/href\=\'([^\']+)\'/', $tag, $src_matches );
if ( $src_matches ) {
$src = $src_matches[1];
}
}
$_src = parse_url( $src );
if ( $_src['query'] ) {
wp_parse_str( $_src['query'], $vars );
foreach ( $vars as $k => &$v ) {
if ( 'ver' !== $k ) {
continue;
}
$v .= '-' . CAC_VERSION;
}
$new_path_and_query = add_query_arg( $vars, $_src['path'] );
$tag = str_replace( $_src['path'] . '?' . $_src['query'], $new_path_and_query, $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'cac_asset_ver', 10, 3 );
add_filter( 'style_loader_tag', 'cac_asset_ver', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment