plugin_dir_path( string $file ): string
Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
Contents
Parameters
-
$file
string Required -
The filename of the plugin (__FILE__).
Return
string the filesystem path of the directory that contains the plugin.
More Information
It is a wrapper for trailingslashit( dirname( $file ) );.
The “plugin” part of the name is misleading – it can be used for any file, and will not return the directory of a plugin unless you call it within a file in the plugin’s base directory.
Source
File: wp-includes/plugin.php
.
View all references
function plugin_dir_path( $file ) {
return trailingslashit( dirname( $file ) );
}
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Including all PHP files from a plugin sub folder and avoiding adding a unnecessary global just to determine a path that is already available everywhere just using WP core functions.
If you want the get the path one level up from the current dir, you can do
Get the directory of the current file:
Top ↑
Feedback
The example comment refers to the return value if the code is called from the root of the
my-plugin
directory. — By Denis Žoljom —Define path constant
For calling numerous files, it is sometimes convenient to define a constant:
Conditional loading
It is sometimes efficient to conditionally load files, e.g., admin-only (or even by specific admin screen):
define( 'PREFIX_BASE_PATH', plugin_dir_path( __FILE__ ) );
define( 'PREFIX_ASSETS_URL', plugins_url( '/assets', __FILE__ ) );
use constant ‘PREFIX_BASE_PATH to include files in functions and files, e.g.,
include( PREFIX_BASE_PATH . 'inc/init.php' );
use constant: ‘PREFIX_ASSETS_URL’ to load assets via url (like; js, css, and images). e.g.,
wp_register_style( 'prefix_library', PREFIX_ASSETS_URL . '/dir/lib.css' );
If you use this function, you can as well just use
trailingslashit( __DIR__ )
. There is literally no point at all in using the wrapper.This is NOT a pendant to what it “pretends” to be (
get_template_directory
), and it is a big negligence that such pendant simply does not exist for plugins.One has to either use
trailingslashit( WP_PLUGIN_DIR . '/your-plugin' )
to get the pendant ofget_template_directory
in a plugin, or create a custom function, if you do not want to use a constant.Top ↑
Feedback
If/until the definition is adjusted to be specific to plugins. Probably won’t happen because of WP’s backwards compatibility standards, but one can hope maybe someday. — By crstauf —