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
thom4
(@oncletom)
Hello,
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%%;
body{
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){
$compiler->setBuffer(str_replace(
array(
‘%%bloginfo_url%%’
‘%%cdn_domain%%’,
‘%%yourtheme_bgcolor%%’,
),
array(
get_bloginfo(‘url’),
aClass::aSpecialMethod(),
get_theme_background_color(),
),
$compiler->getBuffer()
));
}
Plugin Author
thom4
(@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
thom4
(@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.ww.wp.xz.cn/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.
Thanks,
Plugin Author
thom4
(@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.
Thanks,
Plugin Author
thom4
(@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.