Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a query string to the end of the theme's style.css when WordPress is loaded.
<?php
/**
* Add a filter to stylesheet_uri, which appends a query string to it automagically.
*/
add_filter('stylesheet_uri', 'zk_css_versioner');
/**
* the goal of this method is to append a query string to the css url for the site.
* the query string currently is determined by the last time the css file was modified
* the theory is that if the file is modified, change the value of v=, which should
* force the CDN to pull a new version from the origin server. This could be a
* a hit on performance, not sure yet. Let's get it working first.
*
* @return string
*/
function zk_css_versioner() {
$cssfile = get_stylesheet_directory() . "/style.css";
$turi = get_template_directory_uri();
if (file_exists($cssfile)) {
$cssuri = "$turi/style.css?v=" . filemtime($cssfile);
}
echo $cssuri;
}
?>
@vidluther

This comment has been minimized.

Copy link
Owner Author

@vidluther vidluther commented Jun 16, 2012

You should be able to put this file in your mu-plugins folder, and it'll automagically add the string as long you call your stylesheet using bloginfo('stylesheet_uri');

@odlp

This comment has been minimized.

Copy link

@odlp odlp commented Jul 3, 2013

with a single wordpress install (non-MU) this only works changing:

echo $cssuri;

to

return $cssuri;

Otherwise the CSS URI is echoed into the body element.

Neat code though, thanks.

@BHEADRICK

This comment has been minimized.

Copy link

@BHEADRICK BHEADRICK commented Jun 30, 2016

This is awesome, yet so simple!

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.