#6341 closed enhancement (fixed)
feature request: plugin_url() function
Reported by: | Denis-de-Bernardy | Owned by: | jacobsantos |
---|---|---|---|
Milestone: | 2.8 | Priority: | low |
Severity: | minor | Version: | 2.7 |
Component: | Optimization | Keywords: | has-patch tested commit dev-feedback |
Focuses: | Cc: |
Description
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); // domain.com/wp-content/plugins/myplugin/ wp_enqueue_script('my-js', $my_path . 'scripts.js');
it would be handy to include css and js files... thanks.
Attachments (2)
Change History (26)
#7
@
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
@
16 years ago
- Resolution set to worksforme
- Status changed from assigned to closed
I think this is addressed by plugins_url.
#9
@
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.
#11
@
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
@
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
@
16 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
@
16 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
@
16 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..
#17
@
16 years ago
- Summary changed from feature request: plugin_baseurl() function to feature request: plugin_url() function
#18
@
16 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
#20
@
16 years ago
Huh? There is already a function in links-template.php that does this called "plugins_url"
#21
@
16 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...
Something like this already being developed I believe.