定义一个存储插件根路径的常量怎么样?
Define path 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.
-参见
https://developer.wordpress.org/reference/functions/plugin_dir_path/#comment-491因此,在主插件文件中:
define( \'MY_PLUGIN_PATH\', plugin_dir_path( __FILE__ ) );
然后进来
folder/otherfolder/req-file-to-here.php
, 执行:
require MY_PLUGIN_PATH . \'req-file-from-here.php\';
或者,您可以只定义主插件文件的路径:
define( \'MY_PLUGIN_FILE\', __FILE__ );
然后进来
folder/otherfolder/req-file-to-here.php
, 执行:
require plugin_dir_path( MY_PLUGIN_FILE ) . \'req-file-from-here.php\';