涉及到plugin插件类的地址有两个大类,一个是绝对地址,一个是相对地址。
绝对地址有
plugin_dir_url( string $file ),get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.用__FILE__常量的时候当前文件夹的地址,注意以斜杠/ 结尾。用__DIR__常量的时候,是相对插件根目录。
上面生成的是:http://www.example.com/wp-content/plugins/foo/。
plugin_dir_url() 举例:如果要调用的文件是上一级文件夹的,就用DIR常量。
Let’s say current URL is: http://example.com/wp-content/plugins/my-plugin/includes/
1 | echo plugin_dir_url( __FILE__ ). 'images/placeholder.png' ; |
will output: http://example.com/wp-content/plugins/my-plugin/includes/images/placeholder.png
1 | echo plugin_dir_url( __DIR__ ). 'images/placeholder.png' ; |
will output: http://example.com/wp-content/plugins/my-plugin/images/placeholder.png
plugins_url( string $path = ”, string $plugin = ” )
Retrieves a URL within the plugins or mu-plugins directory.
举例:
plugins_url( 'img/bar.jpg' , __FILE__ )
will return a url like http://www.example.com/wp-content/plugins/foo/img/bar.jpg
解释:If no arguments are passed this will deliver the same result as the above function; but with or without a trailing slash at the end. This can be configured to link to files within plugin directory; a useful shortcut.
相对地址有: 用于文件引用当中:
plugin_dir_path() 跟url函数一样,也是以斜杠/结尾。
它引用的是根目录:
$dir= plugin_dir_path( __FILE__);//
Example: /home/user/var/www/wordpress/wp-content/plugins/my-plugin/
其中__FIle__和__DIR__的用法一样。 在当前目录的引用,用__FILE__, 在上一级目录的引用,用__DIR__.