You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enqueued scripts should use plugins_url() instead of plugin_dir_url() #1647
@westonruter I'm not sure I agree with your assessment:
plugin_dir_url() wraps plugins_url(), so the URL up to the plugin's own directory is still filterable.
This means e.g. changing to point to a CDN works just as well with plugin_dir_url().
I don't see any benefit of being able to filter the remainder of the URL (the path relative to the plugin's directory). You can't (or at the very least shouldn't) change the contents of a plugin from the outside, so being able to filter that part makes no sense to me. It's safer not to allow filtering it.
A couple additional use cases for being able to filter the entire URL:
Replace a minified JS or CSS file with a non-minified one, or vice-versa. This requires adding/removing .min from the URL path. (Example filter from Jetpack.)
Add a cache busting query parameter.
Jetpack favors plugins_url() over plugin_dir_url() with 62 files vs 8 files, respectively.
Maybe more importantly, Gutenberg also favors plugins_url() over plugin_dir_url(), with 32 files vs 5 files, respectively.
Thanks @SohamPatel46 for the offer! I think for now we're still discussing whether or not we should make this change. Curious if you have any thoughts on it.
One additional benefit I see with plugin_dir_url() over plugins_url() is that it's better aligned with plugin_dir_path(), which is also commonly needed in plugins. There is no plugins_path() function, so plugins_url() wouldn't be the same. Regarding the ability to filter the entire URL that you mention @westonruter, I guess this means it wouldn't be possible for the CSS/JS file path. Granted, this is probably less important than filtering the URL, but still a potential drawback and at the very least an inconsistency.
From a consistency and ergonomics perspective I'm in favor of keeping plugin_dir_url(), as it aligns with plugin_dir_path(). Additionally, plugins_url() is a lower-level function (as indicated by that it's being used within plugin_dir_url()), and its name is less intuitive to the purpose we'd be using it for, which is to get a specific plugin's asset URL, whereas "plugins URL" sounds more about a URL within the overall wp-content/plugins directory - which still works of course, but it's less intuitive.
As mentioned by @westonruter Jetpack and Gutenberg mostly uses plugins_url() whenever we want a path appended at the end of the URL. So, I think that should be a good practice to use plugins_url() whenever we want to append a path to the URL, rather then manual concatenation to plugin_dir_url()'s output.
Also I think plugin_dir_url() internally uses plugins_url() with keeping an empty path ( ' ' ) as first argument. Ref. So, possibly I dont see any issue upgrading to plugins_url() function.
changed the title [-]Enqueued scripts should use plugins_url() instead of plugin_dir_url()[/-][+]Enqueued scripts should use `plugins_url()` instead of `plugin_dir_url()`[/+]on Dec 18, 2024
@felixarntz On ergonomics point, Though I might not have a suitable verdict on it, but I believe the purpose of the function should matter more than its name. In our case, since plugins_url fits for the purpose more.
Heads up - I am starting work on this, since I have a thumbs up. Thanks !
Activity
felixarntz commentedon Nov 15, 2024
@westonruter I'm not sure I agree with your assessment:
plugin_dir_url()wrapsplugins_url(), so the URL up to the plugin's own directory is still filterable.plugin_dir_url().westonruter commentedon Nov 15, 2024
A couple additional use cases for being able to filter the entire URL:
.minfrom the URL path. (Example filter from Jetpack.)Jetpack favors
plugins_url()overplugin_dir_url()with 62 files vs 8 files, respectively.Maybe more importantly, Gutenberg also favors
plugins_url()overplugin_dir_url(), with 32 files vs 5 files, respectively.SohamPatel46 commentedon Dec 18, 2024
Hello 👋
I would like to work on this issue and update the url function, if its still relevant here.
Thanks.
felixarntz commentedon Dec 18, 2024
Thanks @SohamPatel46 for the offer! I think for now we're still discussing whether or not we should make this change. Curious if you have any thoughts on it.
One additional benefit I see with
plugin_dir_url()overplugins_url()is that it's better aligned withplugin_dir_path(), which is also commonly needed in plugins. There is noplugins_path()function, soplugins_url()wouldn't be the same. Regarding the ability to filter the entire URL that you mention @westonruter, I guess this means it wouldn't be possible for the CSS/JS file path. Granted, this is probably less important than filtering the URL, but still a potential drawback and at the very least an inconsistency.From a consistency and ergonomics perspective I'm in favor of keeping
plugin_dir_url(), as it aligns withplugin_dir_path(). Additionally,plugins_url()is a lower-level function (as indicated by that it's being used withinplugin_dir_url()), and its name is less intuitive to the purpose we'd be using it for, which is to get a specific plugin's asset URL, whereas "plugins URL" sounds more about a URL within the overallwp-content/pluginsdirectory - which still works of course, but it's less intuitive.SohamPatel46 commentedon Dec 18, 2024
Hello @felixarntz,
As mentioned by @westonruter Jetpack and Gutenberg mostly uses
plugins_url()whenever we want a path appended at the end of the URL. So, I think that should be a good practice to useplugins_url()whenever we want to append a path to the URL, rather then manual concatenation toplugin_dir_url()'s output.Also I think
plugin_dir_url()internally usesplugins_url()with keeping an empty path ( ' ' ) as first argument. Ref. So, possibly I dont see any issue upgrading toplugins_url()function.felixarntz commentedon Dec 18, 2024
Makes sense, the transition is certainly possible. Though what do you think about the point I mention on ergonomics?
In any case, it looks like both of you think
plugins_url()works better for this, so I consider myself outvoted. :)[-]Enqueued scripts should use plugins_url() instead of plugin_dir_url()[/-][+]Enqueued scripts should use `plugins_url()` instead of `plugin_dir_url()`[/+]SohamPatel46 commentedon Dec 19, 2024
@felixarntz On ergonomics point, Though I might not have a suitable verdict on it, but I believe the purpose of the function should matter more than its name. In our case, since
plugins_urlfits for the purpose more.Heads up - I am starting work on this, since I have a thumbs up. Thanks !