Skip to content

Instantly share code, notes, and snippets.

@simplistik
Created October 9, 2012 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simplistik/3859017 to your computer and use it in GitHub Desktop.
Save simplistik/3859017 to your computer and use it in GitHub Desktop.
Wordpress: Compile LESS & Wordpress with lessphp
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment