Why is `themeurl` hardcoded?
Closed this issue · 17 comments
In https://github.com/sanchothefat/wp-less/blob/master/wp-less.php#L122 the themeurl
is hardcoded to get_stylesheet_directory_uri()
, why is this not set to the basedir of the enqueued less file? That way, less files can always use relative URLs to their location automatically.
Currently I am hooking into less vars to adjust it, but the filter is only given the handle, so it's not easy to do it automatically.
Let me know if I am misunderstanding that themeurl
is doing.
Thanks!
I'm not sure relative URLs are handled that well yet. From other issues it seems they don't but I haven't confirmed it yet.
It is possible to change/remove themeurl via the filter but I put it in because I tend to have styles in one directory and images in another so the theme root was the common base.
I think the best thing to do would be to add in another variable by default called @lessurl - that way we maintain backwards compatibility and the variable naming still makes sense.
What do you think would be best going forward Joe? Shall I add in a @lessurl
var too or just close this ticket?
@sanchothefat I don't see any real reason for changing that so far. Here's an example how to register an additional theme directory:
function register_new_themes_folder() { register_theme_directory( 'themes_other' ); } add_action( 'plugins_loaded', 'register_new_themes_folder', 0 );
In the themes_other
folder, functions like get_stylesheet_directory()/_uri()
work just fine. Then we also got the filter to influence the location of the LESS files. So far I don't see anything that would make this change worth it. One can register his own directories as vars, etc. From my side: Close vote (without closing it).
I think Joe's point is that it would be better to have URLs in the stylesheets relative to where that stylesheet is eg:
div {
background: url(@{lessurl}/../images/image.png);
}
Although I'm not sure of the immediate advantage to that. Problems arise when additional files are imported as their paths will break. In addition as it's server side lessphp and there is no way to pass in a URL, and given that the compiled CSS files are not in the theme directory it just made sense to provide the theme directory root.
Joe, can you give me some more detail on what it is you do in the less_vars
hook? It might be that the new less_import_dirs
filter could help.
Hey,
I am not 100% of the best thing to do, though the lessurl
seems like a good step. As the path of the less file is passed to wp_enqueue_style()
can one not create the less URL from the dirname
of the path?
Currently there is no way to have less files outside of your theme without hooking into less vars and generating.
Still don't get it. We got the following filters: wp_less_cache_url
, wp_less_cache_path
and less_vars
.
Could you please illustrate (somehow) your scenario where you'd have your less file "outside of your theme"? Maybe that would help. I now labeled it as "question". Willing to change to something other.
Less files are not necessarily in the theme - in plugins secondarily, though I parent themes will also be an issue here, as get_stylesheet_directory()
will be the child theme dir. So currently if you create a childtheme of theme with less, that would stop working.
I don't see how the wp_less_cache_url
and wp_less_cache_path
. It's fine to hook in via PHP in less_vars
-- but my point was maybe that shouldn't be required, as adding the lessurl would overcome that.
Ad Plugins & Styles
Honestly: You shouldn't put something in a plugin that doesn't belong there. Plugins add functionality, Themes add layout.
Ad Filters
This is how I use the filter from within a parent themes functions.php
file. Note, that this is a "pluggable", so it can get overwritten from inside the functions.php
file of a child theme. So this filter perfectly allows having the cached files in parent as well as in a child theme.
add_filter( 'wp_less_cache_url', 'less_cache_url' ); if ( ! function_exists( 'less_cache_url' ) ) : /** * Change the WP LESS cache dir URI * @param string $url * @return string $url */ function less_cache_url( $url ) { if ( is_child_theme() ) return get_stylesheet_directory_uri().'/less-cache'; return get_template_directory_uri().'/less-cache'; } endif;
If you need additional pathes, then use the less_vars
-filter. Here's one example that takes the header image theme mod that was set using the Theme Customizer and the Theme Customization API.
add_filter( 'less_vars', 'default_less_vars' ); /** * Setup LESS variables * @param array $vars * @param string $handle * @return array $vars */ function default_less_vars( $vars, $handle ) { return array_merge( $vars ,array( 'white' => '#FFF' ,'header_img' => sprintf( '~"%s"', get_header_image() ) ) ,get_theme_mod( 'less_options', array() ) ); }
Cheers Joe,
Ill add in lessurl. Check out the latest lessphp docs - there are methods
for adding additional directories to scan imported files for now.
You can also call any of the lessc methods using the action 'lessc'.
Will give you shout when lessurl is added :)
Robert O'Rourke
sanchothefat.com
+44 7816 329424
Honestly: You shouldn't put something in a plugin that doesn't belong there. Plugins add functionality, Themes add layout.
That's way too broad a statement, I needed this specifically because we are using LESS for custom admin pages etc.
The code you provided of course works, this ticket it about ease of use for wp-less
. wp-less adds less support for WordPress enqueued less files, so IMO should do that without the need for PHP hooking to set directories. But really, I don't see your objection to adding a variable that will give you the directory of where the less file is, not just the stylesheet folder.
The statement is indeed too broad. Styling is part of almost every single plugin on wp.org. For projects where you have full control, it makes sense to take that a step further into LESS. Either way, the flexibility should be there for people to be allowed to experiment and try new things. To give real examples of what Joe is talking about:
Cheers
To give real examples of what Joe is talking about:
@noeltock Glad that you dropped in. This (yes, exactly your service) was the real world example I was searching for but couldn't find it, thus not adding an answer.
I agree that there're edge cases that would profit from that addition. As I constantly complain about WP core needing a work-around for each and everything, I withdraw any further attempts to put obstacles in the way of this patch just for the sake of the plugins slimness :)
Cheers for the discussion guys, ill roll this out today.
Robert O'Rourke
sanchothefat.com
+44 7816 329424
@franz-josef-kaiser Awesome :) Thank you Sir!
Last note: I remembered that was also some discussion (which I couldn't dig up) about reworking core styles to use LESS as compiler, so devs would have an easier life reworking the admin UI. Will add it here, if I stumble upon it again.
@noeltock sigh It was you again... :) And Chris Wallace with this ticket.
Aaaaand scene. Update to latest version and @ lessurl is there to use as standard.