Is it safe to pass directory path to plugins_url()?

plugins_url() function accepts plugin slug or file path to build URL for.

I have following directory structure:

/wp-content/mu-plugins/someplugin/css/file.css
/wp-content/mu-plugins/someplugin/includes/file.php

I need to build URL to file.css in file.php. I can’t pass __FILE__ because that will be one level too deep.

plugins_url('css/file.css', __FILE__ )

I can pass __DIR__ to get correct level and it seems to work, but it’s not documented as allowed and I am not sure there isn’t something to bite me later with this.

plugins_url('css/file.css', __DIR__ )

So, is this adequate? Any better way to build URL for these conditions?

1 Answer
1

__DIR__ is rather new and not always supported. Use dirname( __FILE__ ).

plugins_url() is using …

$folder = dirname(plugin_basename($plugin));

… so yes, it is safe.

Just use plugins_url( 'subfolder/file.css', dirname( __FILE__ ) )

Leave a Comment