LESS CSS enqueue_style with add_filter to change rel attribute

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="https://wordpress.stackexchange.com/questions/20876/$href" type="text/less" media="$media" />";
    }
    return $tag;
}
add_filter( 'style_loader_tag', 'enqueue_less_styles', 5, 2);

4 s
4

Yep, final style link output is passed through style_loader_tag filter.

Leave a Comment