我想你在找plugin_dir_url:
$url = plugin_dir_url(__FILE__);
$imageurl = $url.\'images/someimage.png\';
EDIT: 对不起,我误读了这个问题。。。这只是对相关问题的回答。可以递归检查父目录,直到找到正确的目录:
function base_plugin_dir($dirpath) {
if (substr(dirname($dir),-7) == \'plugins\') {return $dirpath;}
else {$dirpath = base_plugin_dir($dirpath);}
return $dirpath;
}
function base_plugin_dir_url($filepath) {
$baseplugindir = base_plugin_dir(dirname($filepath));
$url = plugin_dir_url(trailingslashit($baseplugindir));
return $url;
}
然后,您可以使用该函数从任何文件中获取URL:
$basepluginurl = base_plugin_dir_url(__FILE__);
$imageurl = $basepluginurl.\'/images/wordpress.png\';
OR
更好的是,只需从主插件文件中设置一个常量,以便以后使用:
$url = plugin_dir_url(__FILE__);
define(\'MY_UNIQUE_PLUGIN_URL\',$url);
以便在say中使用
/plugin/my-plugin/class/class1.php
$imageurl = MY_UNIQUE_PLUGIN_URL.\'/images/wordpress.png\';