Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#32519 closed defect (bug) (invalid)

Enqueueing styles with directory separator

Reported by: doecode's profile doecode Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2
Component: Script Loader Keywords:
Focuses: Cc:

Description

Before running a specific task all my assets are within a directory called "tmp". To get the right directory for enqueuing styles I'm using the following function:

function asset_path($dir = '') {
    $path = '/';
    if(locate_template('tmp')) {
        $path .= 'tmp/';
    }
    $path .= 'assets' . '/';
    $path .= $dir . '/';

    return $path;
}

So wp_enqueue_style('vendor', get_template_directory_uri() . asset_path('vendor') . 'vendor.css') works fine. If I change the asset_path function to

if( ! defined('DS') ? define('DS', DIRECTORY_SEPARATOR) : null);

function asset_path($dir = '') {
    $path = DS;
    if(locate_template('tmp')) {
        $path .= 'tmp' . DS;
    }
    $path .= 'assets' . DS;
    $path .= $dir . DS;
    return $path;
}

it doesn't work anymore, since it seems that WordPress replaces the "\" (currently working on Windows OS) with "". So my request looks like ...wp-content/themes/boilerplatetmpassetsvendorvendor.css?ver=4.1.4 .

I didn't find anything related to this behaviour, hence I'm assuming it's a bug.

Another strange behaviour is that if I use the hardcoded directory separators and try to find out if the target exists with

if(locate_template(get_template_directory_uri() . asset_path('vendor') . 'vendor.css'))
// ...
}

wp_enqueue_style('vendor', get_template_directory_uri() . asset_path('vendor') . 'vendor.css');

the condition fails - but enqueuing succeeds.

Change History (1)

#1 @rmccue
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Thanks for reporting this issue! :)

wp_enqueue_* takes a URL rather than a path, and \ is not a valid character in URLs (but can be encoded as %5C). These enqueued URLs are then passed through URL sanitisation (esc_url), which strips invalid characters.

You can pass your paths through wp_normalize_path to convert backslashes to forward slashes, however keep in mind that you typically don't want to bother with DIRECTORY_SEPARATOR anyway. Windows can handle forward slashes as directory (folder) separators anyway, so it helps to simplify your code a bit.

Note: See TracTickets for help on using tickets.