Created
September 17, 2012 02:41
-
-
Save jtallant/3735275 to your computer and use it in GitHub Desktop.
Automatically minify css if it's been updated.
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
| <?php | |
| /** | |
| * Automatically minifies style.css and writes output to style.min.css | |
| * | |
| * @author Justin Tallant | |
| * @param boolean $minification_is_on when true ends execution of the function | |
| * @return void | |
| * @link http://justintallant.com/switching-between-minified-and-non-minified-css-and-js-files-in-wordpress/ | |
| */ | |
| function jt_minify_css($minification_is_on = true) { | |
| // if minification is on we shouldn't be editing the non-minified stylesheet | |
| // so there is no reason to check if it has been updated | |
| if ( true == $minification_is_on ) { | |
| return; | |
| } | |
| $last_modified = filemtime( get_stylesheet_directory() . '/style.css' ); | |
| add_option( '_stylesheet_last_modified_', $last_modified ); | |
| if ( get_option('_stylesheet_last_modified_') != $last_modified ) { | |
| update_option('_stylesheet_last_modified_', $last_modified); | |
| $command = 'cleancss -o ' . get_stylesheet_directory() . '/style.min.css ' . get_stylesheet_directory() . '/style.css'; | |
| exec($command); | |
| } | |
| } | |
| jt_minify_css($jt_use_minified_files); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This won't work unless you have Node installed on your server and have installed the clean-css package.
See this blog post for details http://justintallant.com/switching-between-minified-and-non-minified-css-and-js-files-in-wordpress/