What I’m trying to do is use less css with WordPress.
You’re supposed to link to your .less files with the rel attribute set to ‘stylesheet/less’. But I can’t figure out how to alter the code that enqueue_style outputs.
Is there a way to apply a filter and affect the output?
EDIT: If anyone is curious as to how I ended up getting this to work, here is the code snippet:
function enqueue_less_styles($tag, $handle) {
global $wp_styles;
$match_pattern = '/.less$/U';
if ( preg_match( $match_pattern, $wp_styles->registered[$handle]->src ) ) {
$handle = $wp_styles->registered[$handle]->handle;
$media = $wp_styles->registered[$handle]->args;
$href = $wp_styles->registered[$handle]->src . '?ver=' . $wp_styles->registered[$handle]->ver;
$rel = isset($wp_styles->registered[$handle]->extra['alt']) && $wp_styles->registered[$handle]->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
$title = isset($wp_styles->registered[$handle]->extra['title']) ? "title='" . esc_attr( $wp_styles->registered[$handle]->extra['title'] ) . "'" : '';
$tag = "<link rel='stylesheet' id='$handle' $title href='$href' type='text/less' media='$media' />";
}
return $tag;
}
add_filter( 'style_loader_tag', 'enqueue_less_styles', 5, 2);
Yep, final style link output is passed through
style_loader_tag
filter.I made a function that uses the query() method of the WP_Dependancies class. Furthermore it does not regenerate the output, but instead just rewrites the necessary parts.
The function accesses the global $wp_styles object and performs a query to get the stylesheet object. With the regex the src is checked if it contains a .less file, and if that is true, the rel attribute is modified accordingly. In my function I furthermore replaced the -css suffix in the ID with a -less suffix, just remove this line if you don’t like it.
change
rel=stylesheet
torel=stylesheet/less
in$tag
definition.. alsorel=alternate stylesheet/less
, doesn’t work..Thanks for your answer. It didn’t work for me until I put echo in place of return though :