Filter to force compile #39

merged 1 commit into from Mar 6, 2013


None yet
4 participants

bradyvercher commented Mar 4, 2013

Changes made to functions or variables registered in PHP, such as when working with the Theme Customizer, don't cause LESS files to be recompiled, so the changes aren't visible in the Customizer preview or the front-end unless a LESS file is updated manually.

A second parameter is available in the lessc::cachedCompile() method for forcing the files to be rebuilt, so this filter allows it to be updated when necessary.

roborourke added a commit that referenced this pull request Mar 6, 2013

@roborourke roborourke merged commit 8c68692 into roborourke:master Mar 6, 2013


roborourke commented Mar 6, 2013

Thanks Brady, that's probably the simplest way to force compiling. I was imagining something like accessing the lessc class instance directly to do that but in wordpress terms a lot easier to add/remove a filter when necessary.


bradyvercher commented Mar 6, 2013

Ahh, I see. That wasn't immediately obvious, but makes sense. I was thinking of just toggling the filter when necessary using __return_true or __return_false. Thanks for the merge.


Please pardon my ignorance, but I'm confused as how to use this.

I'm using ACF Lite for my theme options (which I'll need to update soon, I know), and I can hook into acf_save_post to do something when the options are saved, but I'm not sure how exactly to use this filter in there.

function updateCss() {
add_action('acf_save_post', 'updateCss', 20);

I'm a bit novice, so please let me know if I'm way off base.


bradyvercher commented Apr 5, 2013

Hi @amandabourbois. This filter only works when the plugin checks the LESS style sheet for changes, which is just before they're output to the page, so it won't actually do anything otherwise. In your case, what you would need to do is set a flag when your options are saved so you can force the style sheets to be recompiled the next time they're requested.

Here's how I envisioned it working:

function prefix_acf_save_post() {
    // Set a flag to recompile LESS on the next front end request.
    update_option( 'prefix_force_recompile', 'yes' );
add_action( 'acf_save_post', 'prefix_acf_save_post' );

Then in a hook on the frontend before the style sheet is output, you would check the flag, set the filter if needed, then reset the flag.

function prefix_check_acf_save_flag() {
    if ( 'yes' == get_option( 'prefix_force_recompile' ) ) {
        // Force the LESS style sheets to be recompiled.
        add_filter( 'less_force_compile', '__return_true' );

        // Reset the flag so the style sheets won't be recompiled on every request.
        delete_option( 'prefix_force_recompile' );
add_action( 'wp_enqueue_script', 'prefix_check_acf_save_flag' );

That helps a lot, thanks!


roborourke commented Apr 15, 2013

Including your less vars via the hooks described on the readme should cause it to recompile as well. Alternatively you can compile any output on the fly using the lessc class directly if you need to.

usaphp commented Jul 3, 2013

  • "Including your less vars via the hooks described on the readme should cause it to recompile as well."

@sanchothefat that does not work for me - when I change a variable value inside of that php hook - it does not recompile, but when I change something in .less file - that triggers a recompile. Is there anyway to forse it to recompile if a variable is changed in the hook? (I am talking about the hook from readme file)

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