Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tillkruss
Created August 12, 2014 04:29
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 tillkruss/8c71b24165b18db97439 to your computer and use it in GitHub Desktop.
Save tillkruss/8c71b24165b18db97439 to your computer and use it in GitHub Desktop.
add query string cache buster to stylesheets minified by W3 Total Cache
<?php
// add query string cache buster to W3TC minified stylesheet links
add_action( 'init', function() {
// is css minify enabled?
if ( isset( $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ] ) && $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ][0]->_config->get_cache_option( 'minify.css.enable' ) ) {
// store original minify callback
$GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify-org' ] = $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ];
// replace minify callback
$GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ] = function( &$buffer ) {
// run original minify callback
$buffer = call_user_func( $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify-org' ], $buffer );
// add `?ver=<your-cache-buster>` cache buster to stylesheet link
$buffer = preg_replace( '~<link(.+)href="(.+\/cache\/minify\/.+)"(.+)\/>~U', '<link$1href="$2?ver=<your-cache-buster>"$3/>', $buffer );
return $buffer;
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment