public
Last active

Wordpress: Compile LESS & Wordpress with lessphp

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
/**
* Compile a LESS file in Wordpress with lessphp while preserving the comment meta.
* @param string $less_fname LESS input file path
* @param string $css_fname CSS output file path
*/
function auto_compile_less($less_fname, $css_fname)
{
// WP 3.4 Setup
$theme_meta = array();
 
if ( wp_get_theme()->get('Name') )
$theme_meta[] = 'Theme Name: ' . wp_get_theme()->get('Name');
if ( wp_get_theme()->get('ThemeURI') )
$theme_meta[] = 'Theme URI: ' . wp_get_theme()->get('ThemeURI');
if ( wp_get_theme()->get('Description') )
$theme_meta[] = 'Description: ' . wp_get_theme()->get('Description');
if ( wp_get_theme()->get('Version') )
$theme_meta[] = 'Version: ' . wp_get_theme()->get('Version');
if ( wp_get_theme()->get('Author') )
$theme_meta[] = 'Author: ' . wp_get_theme()->get('Author');
if ( wp_get_theme()->get('AuthorURI') )
$theme_meta[] = 'Author URI: ' . wp_get_theme()->get('AuthorURI');
 
$theme_data = "/*\n" . implode("\n", $theme_meta) . "\n*/\n";
$cache_fname = $less_fname . '.cache';
 
if ( file_exists($cache_fname) ):
$cache = unserialize(file_get_contents($cache_fname));
else:
$cache = $inputFile;
endif;
 
$less = new lessc;
$new_cache = $less->cachedCompile($cache);
 
if (!is_array($cache) || $new_cache['updated'] > $cache['updated']):
file_put_contents($cache_fname, serialize($new_cache));
file_put_contents($css_fname, $theme_data . $new_cache['compiled']);
endif;
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.