Skip to content

Instantly share code, notes, and snippets.

@grappler
Forked from isGabe/functions.php
Created November 18, 2012 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grappler/4107096 to your computer and use it in GitHub Desktop.
Save grappler/4107096 to your computer and use it in GitHub Desktop.
Auto versioning of CSS/JS files in WordPress
/*
Auto-version CSS & JS files, allowing for cache busting when these files are changed.
Place in functions.php or wherever you are enqueueing your scripts & styles
Avoids using query strings which prevent proxy caching
Adjust paths based on your theme setup. These paths work with Bones theme
*/
$mtime = filemtime(dirname(__FILE__) . '/css/style.css');
wp_register_style( 'bones-stylesheet', get_stylesheet_directory_uri() . '/library/css/style.' . $mtime . '.css', array(), null, 'all');
// enqueue the stylesheet
wp_enqueue_style( 'bones-stylesheet' );
/*
this will change css filename from style.css to style.1234567890.css, where 1234567890 is the time that the file was last saved.
IMPORTANT: Unless you use the .htaccess rewrite rule below (or something similar), this will break your site!
Feel free to throw things at me if this isn't done correctly. I took the idea from here:
http://w-shadow.com/blog/2012/07/30/automatic-versioning-of-css-js/
*/
# place this .htaccess file in your theme directory
# For now I cannot get this to work with WP Engine. It works fine locally (MAMP)
# Auto-versioning support
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment