plugin_dir_path()
云策文档标注
概述
plugin_dir_path() 函数用于获取传入文件(通常为 __FILE__)所在目录的文件系统路径,并自动添加尾部斜杠。它本质上是 trailingslashit( dirname( $file ) ) 的包装器,名称中的“plugin”可能具有误导性,因为它可用于任何文件,并不专门针对插件。
关键要点
- 参数:必需一个字符串 $file,通常是 __FILE__,表示插件或任何文件的文件名。
- 返回值:返回包含该文件的目录的文件系统路径,带尾部斜杠。
- 内部实现:直接调用 trailingslashit( dirname( $file ) ),无额外逻辑。
- 注意事项:函数名暗示插件相关,但实际适用于任何文件;要获取插件根目录路径,需在插件基础目录内的文件中调用。
- 相关函数:trailingslashit() 用于添加尾部斜杠。
- 版本历史:自 WordPress 2.8.0 引入。
代码示例
// 获取当前文件所在目录路径
$dir = plugin_dir_path( __FILE__ );
// 示例输出:/home/user/var/www/wordpress/wp-content/plugins/my-plugin/
// 获取上一级目录路径
$dir = plugin_dir_path( __DIR__ );
// 示例输出:/home/user/var/www/wordpress/wp-content/plugins/
// 定义路径常量以便复用
define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'includes/admin-page.php' );
// 使用 glob 包含子文件夹中的所有 PHP 文件
foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) {
include_once $file;
}
// 条件加载文件(如仅管理员)
if ( is_admin() ) {
include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' );
} else {
include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' );
}注意事项
用户反馈指出,该函数仅是 trailingslashit( __DIR__ ) 的包装器,可能无额外价值;且缺乏类似 get_template_directory() 的插件专用函数,建议使用 trailingslashit( WP_PLUGIN_DIR . '/your-plugin' ) 或自定义函数作为替代。
原文内容
Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
Parameters
$filestringrequired-
The filename of the plugin (__FILE__).
Source
function plugin_dir_path( $file ) {
return trailingslashit( dirname( $file ) );
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 9 content
Aurovrata Venet
If you want the get the path one level up from the current dir, you can do
//current path: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/ $dir = plugin_dir_path( __DIR__ ); //$dir is set to /home/user/var/www/wordpress/wp-content/plugins/Skip to note 10 content
Codex
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.
foreach ( glob( plugin_dir_path( __FILE__ ) . "subfolder/*.php" ) as $file ) { include_once $file; }Skip to note 11 content
Codex
Get the directory of the current file:
$dir = plugin_dir_path( __FILE__ ); // Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/my-plugindirectory.Skip to note 12 content
Codex
Define path constant
For calling numerous files, it is sometimes convenient to define a constant:
define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); include( MY_PLUGIN_PATH . 'includes/admin-page.php'); include( MY_PLUGIN_PATH . 'includes/classes.php'); // etc.Skip to note 13 content
Codex
Conditional loading
It is sometimes efficient to conditionally load files, e.g., admin-only (or even by specific admin screen):
if ( is_admin() ) { include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' ); } else { include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' ); }Skip to note 14 content
Anonymous User
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_directoryin a plugin, or create a custom function, if you do not want to use a constant.Skip to note 15 content
bharatthapa
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' );Skip to note 16 content
raihanbabuam2am
if ( is_admin() ) { include_once( plugin_dir_path( __FILE__ ) . 'includes/admin-functions.php' ); } else { include_once( plugin_dir_path( __FILE__ ) . 'includes/front-end-functions.php' ); }