函数文档

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__).

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

function plugin_dir_path( $file ) {
	return trailingslashit( dirname( $file ) );
}

Changelog

Version Description
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 13 content

    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' );
    }

  2. 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 of get_template_directory in a plugin, or create a custom function, if you do not want to use a constant.

  3. Skip to note 15 content

    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' );