Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#55522 new enhancement

Misleading method names plugin_dir_path and plugin_dir_url

Reported by: subrataemfluence's profile subrataemfluence Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9.2
Component: Plugins Keywords: has-patch
Focuses: Cc:

Description

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
/**
 * 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
/**
 * 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() );
}

Attachments (1)

55522.patch (1.6 KB) - added by subrataemfluence 2 years ago.
Suggested enhancement

Download all attachments as: .zip

Change History (3)

@subrataemfluence
2 years ago

Suggested enhancement

#1 @joyously
2 years ago

What is the problem you are trying to solve?
I think you can't make these changes or you will break some code.
The change for using WP_CONTENT_DIR . '/plugins' is bad, since the plugins folder could have been changed to something else using define( 'WP_PLUGIN_DIR', 'somethingelse' ); unrelated to WP_CONTENT_DIR or /plugins.

The change for plugin_dir_url makes the same call either way, which is the default for those parameters, so why change it?

Last edited 2 years ago by joyously (previous) (diff)

#2 @sabernhardt
2 years ago

  • Focuses accessibility removed
Note: See TracTickets for help on using tickets.