Support » Plugins and Hacks » [Plugin: WP-LESS] using php inside a .less file

[Plugin: WP-LESS] using php inside a .less file

  • hi there,

    that’s a really great plugin and I’m super happy to use it, I’m sure it’ll cut my dev time quite a bit, thanks!

    what I wanted to ask is if it’s possible to have php values echoed inside the .less file, I’m considering using it in conjunction with some theme-options page where the user would be able to choose a colour/size etc. right now I get an error..

    what do you think about it?

    thanks again!


Viewing 11 replies - 1 through 11 (of 11 total)
  • bump?

    Ken Newman


    I was hoping for an implementation would take options (colors, font-family, padding, etc.) and pass those to the lessphp compiler, which on option update (wp api) would compile to static css, which would be included in the theme, rather then the .less file. Would work better with the boot-strap I think… I know how to do options pages, and I think I can do the boot-strap, but like obvio, I was wondering how tough passing php variables to lessphp is?

    Plugin Author oncletom



    the LESS API can’t let us doing such things. So sorry, we can’t plug directly.

    However, there are many hooks and filter you can deal with to achieve your goal. I realized it’s a bit complicated to do that so I added a wp-less_compiler_parse, plus 2 new methods on the compiler, getBuffer and setBuffer.

    This way, you can alter to loaded LESS file before it is pared and outputed in a pure CSS file:

    @siteurl: %%bloginfo_url%%;
    @cdn_doman: %%cdn_domain%%;
    @bgcolor: %%yourtheme_bgcolor%%;

    background: @bgcolor url(‘@cdn_domain/specialdir/background.png’);

    And in a PHP function file, or whatever name it has:

    add_action(‘wp-less_compiler_parse’, ‘my_plugin_less_replace_vars’);

    function my_plugin_less_replace_vars(WPLessCompiler $compiler){

    Plugin Author oncletom


    If needed, I can also add some handy methods to cleanup existing compiled files.

    So in one line of code, with your own plugin, you could manage to purge static files.

    Keep me in touch.

    When I try to use this method of passing variables to the .less file I get this:
    parse error: failed at @siteurl: %%bloginfo_url%%; line: 2

    Plugin Author oncletom


    This means your replacements are done after the parsing of LESS file. It should be before any content is parsed.

    If you do it with the filter, it’s too late (it’s the returned parsed content).
    If you do it with the action, you handle the Compiler before it’s content get parsed.

    Check this line: http://plugins.trac.wordpress.org/browser/wp-less/trunk/lib/Compiler.class.php?rev=313122#L36

    No matter where I put the action call, I get that same error. I’ve tried placing it before the less sheet is processed as well as after. It doesn’t seem to be replacing the content, I’ve used your exact example and received these results. Any help would be appreciated.


    Plugin Author oncletom


    Upcoming 1.3.1 release will fix the problem.
    I just changed action names, to avoid collisions with filter.

    I may alter some things later on this part, to avoid accessing the buffer this way.

    What would the syntax be for the new modifications to pass these variables? I’ve been trying several things using the new action hook names but I’ve not been able to get anything different than before.


    Plugin Author oncletom


    It’s exactly the same code, except you have to hook on wp-less_compiler_parse_pre.

    I’ve tested and it worked.

    I did get it working, but I had to make two changes in order to make it work:
    1. It complains about parsing errors with:
    @siteurl: %%bloginfo_url%%;
    but it works with:
    @siteurl: ‘%%bloginfo_url%%’;

    2. Variables that are in imported files do not get replaced.

    In order to pass variables to different files I have to import the files, if I enqueue them each individually, the variables do not get passed.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Plugin: WP-LESS] using php inside a .less file’ is closed to new replies.