WordPress文档Determining Plugin and Content Directories 声明:
WordPress在确定内容和插件目录的路径时使用以下常量。These should not be used directly by plugins or themes, 但出于完整性考虑,此处列出了。
接下来是列表WP_CONTENT_DIR
和WP_PLUGIN_DIR
在主题和插件开发人员不应使用的常量中,可能是因为以下原因:
WordPress允许用户将其wp内容目录放置在任何他们想要的地方,因此您绝不能假设插件将位于wp内容/插件中,或者上传将位于wp内容/上传中,或者主题将位于wp内容/主题中。
Mark Jaquith也comments here 不应使用这些常量:
不要使用WP\\u PLUGIN\\u URL或WP\\u PLUGIN\\u DIR-插件可能不在插件目录中。
So, what is the accepted way of referencing the full path to the plugins, wp-content, and themes folders without using these constants?
作为一个简单的示例,要输出所有已安装插件的完整路径,我可以这样做:<?php
$plugins = get_plugins();
foreach ($plugins as $file => $details) {
echo WP_PLUGIN_DIR . \'/\' . $file . \'<br>\';
}
生成如下列表:/var/www/wp-content/plugins/akismet/akismet.php
/var/www/wp-content/plugins/debug-bar/debug-bar.php
/var/www/wp-content/plugins/hello.php
(例如,如果我正在编写一个插件,允许用户有选择地将插件归档为站点备份的一部分,我可能会这样做。)如果使用WP_PLUGIN_DIR
如果错误,建议的替代方案是什么?没有等效于wp_upload_dir()
对于我可以找到的plugins、themes和wp content文件夹,这使得引用潜在的漫游主题和插件根目录成为问题。