Created
June 16, 2015 13:50
-
-
Save boonebgorges/1c46f06939b701a1ef43 to your computer and use it in GitHub Desktop.
Custom version querystring appending for WordPress JS and CSS assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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