Create a gist now

Instantly share code, notes, and snippets.

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', 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