-
-
Save grappler/4107096 to your computer and use it in GitHub Desktop.
Auto versioning of CSS/JS files in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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/ | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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