[wp-trac] [WordPress Trac] #55522: Misleading method names plugin_dir_path and plugin_dir_url
WordPress Trac
noreply at wordpress.org
Mon Apr 4 05:43:45 UTC 2022
#55522: Misleading method names plugin_dir_path and plugin_dir_url
------------------------------+-----------------------------
Reporter: subrataemfluence | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Plugins | Version: 5.9.2
Severity: normal | Keywords: has-patch
Focuses: accessibility |
------------------------------+-----------------------------
The method names `plugin_dir_path` and `plugin_dir_url` names are very
misleading. Both the methods, as their names suggest should either return
the path and URL of the base plugin directory i.e. `wp-content/plugins` or
a specific plugin directory based on plugin file path that is supplied in
the methods.
But in practice, they are designed to reurn any path starting from
application root to theme and everything. Logically this is fine but
ideally the behaviour should match the method names!
The best option would be to have different method names for differnt
purposes, e.g. `plugin_dir_path` and `file_path` although they are doing
the same thing!
But adding two new methods, that too for doing the same thing might not be
a good idea. So I would suggest the following so that at least the methods
can return base plugin dierectory pth and URL with no or empty value
`_FILE_` is passed.
{{{#!php
<?php
/**
* Get the filesystem directory path (with trailing slash) for the plugin
__FILE__ passed in.
*
* @since 5.9.2
*
* @param string $file The filename of the plugin (__FILE__). Default
empty
* @return string the filesystem path of the directory that contains the
plugin if a filename is specified. Returns the base plugin directory path
otherwise.
*/
function plugin_dir_path( $file = '' ) {
return trim( $file ) !== ''
? trailingslashit( dirname( $file ) )
: trailingslashit( WP_CONTENT_DIR . '/plugins' );
}
}}}
{{{#!php
<?php
/**
* Get the URL directory path (with trailing slash) for the plugin
__FILE__ passed in.
*
* @since 5.9.2
*
* @param string $file The filename of the plugin (__FILE__). Default
empty
* @return string the filesystem path of the directory that contains the
plugin if a filename is specified. Returns the base plugin directory path
otherwise.
*/
function plugin_dir_url( $file = '' ) {
return trim( $file ) !== ''
? trailingslashit( plugins_url( '', $file ) )
: trailingslashit( plugins_url() );
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55522>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list