Skip to content

Instantly share code, notes, and snippets.

@carestad
Forked from vidluther/cssversioner.php
Created October 5, 2012 19:56
Show Gist options
  • Save carestad/3841998 to your computer and use it in GitHub Desktop.
Save carestad/3841998 to your computer and use it in GitHub Desktop.
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', function($css) {
$parts = parse_url($css);
$file = $_SERVER['DOCUMENT_ROOT'] . $parts['path'];
if (file_exists($file)) {
$query = array();
if (isset($parts['query'])) {
parse_str($parts['query'], $query);
}
$query['ver'] = filemtime($file);
$querystr = http_build_query($query, null, '&amp;');
$user = isset($parts['user']) ? $parts['user'] : '';
$pass = isset($parts['pass']) ? $parts['pass'] : '';
$userpass = (!empty($user) || !empty($pass)) ? $user.'@'.$pass : '';
$css = "{$parts['scheme']}://{$userpass}{$parts['host']}{$parts['path']}?{$querystr}";
echo $css;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment