Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created September 17, 2012 02:41
Show Gist options
  • Select an option

  • Save jtallant/3735275 to your computer and use it in GitHub Desktop.

Select an option

Save jtallant/3735275 to your computer and use it in GitHub Desktop.
Automatically minify css if it's been updated.
<?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);
@jtallant
Copy link
Copy Markdown
Author

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment