Make WordPress Core

Opened 16 years ago

Closed 15 years ago

Last modified 15 years ago

#6341 closed enhancement (fixed)

feature request: plugin_url() function

Reported by: denis-de-bernardy's profile Denis-de-Bernardy Owned by: jacobsantos's profile jacobsantos
Milestone: 2.8 Priority: low
Severity: minor Version: 2.7
Component: Optimization Keywords: has-patch tested commit dev-feedback
Focuses: Cc:


function plugin_url($file) {
  $url = plugin_basename($file);
  $url = preg_replace("/[^\/]+$/", '', $url); // not dirname(), as it returns '.' for Dolly
  $url = trailingslashit(get_option('siteurl') . 'wp-content/plugins/' . $url;
  return $url;

$my_path = plugin_url(__FILE__);
var_dump($my_path); //

wp_enqueue_script('my-js', $my_path . 'scripts.js');

it would be handy to include css and js files... thanks.

Attachments (2)

6341.r9125.diff (4.7 KB) - added by jacobsantos 16 years ago.
completes feature request based off of r9125
6341.diff (1018 bytes) - added by Denis-de-Bernardy 15 years ago.
works with dolly and plugins in a subfolder

Download all attachments as: .zip

Change History (26)

#1 @DD32
16 years ago

  • Keywords needs-patch added
  • Milestone set to 2.6

#2 @Nazgul
16 years ago

  • Type changed from defect to enhancement

#3 @lloydbudd
16 years ago

  • Milestone changed from 2.6 to 2.7

#4 @jacobsantos
16 years ago

  • Owner changed from anonymous to jacobsantos

Something like this already being developed I believe.

#5 @jacobsantos
16 years ago

  • Status changed from new to assigned

16 years ago

completes feature request based off of r9125

#6 @jacobsantos
16 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

#7 @DD32
16 years ago

Keep in mind that plugins_url() exists.

It creates a URL based on $url = plugins_url('my-plugin-folder/style.css');

Maybe instead, that function should be extended as such:

$url = plugins_url('style.css', __FILE__);
function plugins_url($file = '', $relative = '') {
if ( ! empty($relative) )
    $url .= dirname(plugin_basename($relative))
$url .= $file;

You get the idea..? In other words, have a 2nd param to have the URL created for hte plugins directory, but relative to the current plugins folder.

#8 @matt
16 years ago

  • Resolution set to worksforme
  • Status changed from assigned to closed

I think this is addressed by plugins_url.

#9 @DD32
16 years ago

I think this is addressed by plugins_url.

It still however requires the plugin author to determine the folder its installed in, and then pass that folder to plugins_url() along with the file its after, Streamlining that with a "relative to dirname(FILE)" might be a good idea though.

#10 @jacobsantos
16 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

#11 @jacobsantos
16 years ago

The idea is so that you just put in FILE and get the relative base directory back.

function plugin_baseurl($path) {
    return plugins_url( plugin_basename( dirname($path) ) );

So you would just need to do $url = plugin_baseurl(__FILE__); .

#12 @jacobsantos
16 years ago

  • Keywords has-patch needs-testing removed
  • Milestone 2.7 deleted
  • Resolution set to invalid
  • Status changed from reopened to closed

Actually, you know, if you want the base, it should be a constant and done once. The code above is simple enough that an entire function for it shouldn't be needed. Perhaps if plugins_url() did not exist before. It does exist and works very well with its current code.

#13 @Denis-de-Bernardy
15 years ago

  • Milestone set to 2.8
  • Version changed from 2.5 to 2.7

well, using this plugin, it goes:

$folder = plugins_url() . '/' . basename(dirname(FILE));

kinda sucks imo, but I guess I can live with that...

I still think there is room for:

function plugin_baseurl($FILE) {
    return plugins_url() . '/' . basename(dirname($FILE));

usage: $file = plugin_baseurl(FILE) . '/js/myscript.js';

#14 @Denis-de-Bernardy
15 years ago

adding to this one, the above code isn't even relevant in some cases. picture a user installing a plugin that was supposed to be in a folder, in the plugins folder directly. i.e. wp-content/plugins/foo.php rather than wp-content/plugins/foo/foo.php. or, in the case of wpmu, something like: wp-content/mu-plugins/foo/foo.php

I doubt plugins_url() or anything using it will fix the problem of extracting the "true" plugin folder.

#15 @DD32
15 years ago

I'm still +1 for extending plugins_url() to take a 2nd param of __FILE__ so that the URL returned from the function is relative to the folder, ie:

$url = plugins_url('script.js', __FILE__);
echo $url; //http://...../wp-content/plugins/my-folder/script.js
$url = plugins_url();
echo $url; //http://..../wp-content/plugins/

It'll be a shame to add yet another function..

15 years ago

works with dolly and plugins in a subfolder

#16 @Denis-de-Bernardy
15 years ago

  • Keywords has-patch tested added

#17 @Denis-de-Bernardy
15 years ago

  • Summary changed from feature request: plugin_baseurl() function to feature request: plugin_url() function

#18 @Denis-de-Bernardy
15 years ago

I really don't like the idea of a relative parameter for that.

it's like... not natural, from a syntax standpoint, to call:

$folder = plugins_url('', __FILE__);
// and then batch enqueue script/css files

this is more natural:

$folder = plugin_url(__FILE__);
// and then batch enqueue script/css files

#19 @Denis-de-Bernardy
15 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

#20 @GamerZ
15 years ago

Huh? There is already a function in links-template.php that does this called "plugins_url"

#21 @Denis-de-Bernardy
15 years ago

  • Keywords dev-feedback added

Not so... plugin_urls() gives you the url of the plugin dir, not that of the plugin.

Surely, you've authored enough plugins to know that, while you develop your plugin as if it was going to go into wp-content/plugins/yourfolder, there is a strong likelihood that those who download it will install it in wp-content/plugins, or wp-content/otherfolder instead...

Plus, having such a plugin_url() function makes good sense to ensure that plugins are compatible with WPMU. if its WPMU version, plugin_url(FILE) would/should additionally check if the plugin is in wp-content/plugins or wp-content/mu-plugins -- and return the correct url no matter what...

#22 @jacobsantos
15 years ago

Wishes this would be committed.

#23 @jacobsantos
15 years ago

  • Keywords commit added

#24 @westi
15 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [10578]) Extend plugins_url to allow you to specify the plugin file you want to be relative to so slugs don't need to be hardcoded. Fixes #6341.

Note: See TracTickets for help on using tickets.