Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.