roborourke/wp-less

Allow changing the wp-less-cache dir location

Closed this issue · 4 comments

Example: I want to use Twitter Bootstrap together with WP Less.

The problem: Referenced icons/urls. Due to the fact, that WP Less is using the wp_upload_dir() function, I've no chance to use the default references. Going into the Bootstrap files and changing the location also is no option, as this would prevent updates without Bootstrap core changes.

One solution would be to use the filter in the WP core function, but I don't know how I could check if the actual call comes from WP Less, to not interrupt other parts of WP.

IMHO the best solution (I can imagine) would be a filter to move the cache folder location around.

Agreed, would be good to get it working nicely with bootstrap. Cheers for
the heads up :)

I already tried a filter and it works without any problems (for me):

// Inside wp-less.php
$upload_dir = apply_filters( 'wp_less_chache_dir', wp_upload_dir() );

// Inside functions.php
/**
 * Change the WP LESS cache dir
 * 
 * @param array $data
 * @return array $data
 */
function custom_less_cache_dir( $data )
{
    $data[ 'basedir' ] = get_stylesheet_directory().'/lib/bootstrap';
    $data[ 'baseurl' ] = get_stylesheet_directory_uri().'/lib/bootstrap';

    return $data;
}
add_filter( 'wp_less_chache_dir', 'custom_less_cache_dir' );

I don't know how convenient this is, as you also got the possibility to change other stuff. On the other hand, it doesn't affect anything).

I've added 2 direct filters so you can do the following:

<?php

add_filter( 'wp_less_cache_path', create_function( '$a', 'return get_stylesheet_directory()."/lib/bootstrap"' ) );
add_filter( 'wp_less_cache_url', create_function( '$a', 'return get_stylesheet_directory_uri()."/lib/bootstrap"' ) );

?>

Modifying the array from the wp_upload_dir() function requires you to know its structure -easy to find out I know but you have to edit the 2 values anyway. In addition with the latest PHP versions you wouldn't even need the create_function(), just an anonymous function.

I avoid using anonymous/lambda functions, as I can't remove them from action/filters later on.

Thanks anyway for the update - will update and report back.