WordPress 插件路径相关

WordPress的插件目录在 wp-content/plugins/ 下,一个插件一个目录,安装插件时可以把解压的插件上传到这个目录下或者在wordpress后台安装插件。

plugins_url()

echo plugins_url();
//输出:https://rangtuo.com/wp-content/plugins
echo plugins_url('',__FILE__);
//输出:https://rangtuo.com/wp-content/plugins/myplugin
echo plugins_url('js/myscript.js',__FILE__);
//输出:https://rangtuo.com/wp-content/plugins/myplugin/js/myscript.js
plugin_dir_url()
返回当前插件的目录 URI,例如
echo plugin_dir_url(__FILE__ );
//输出:https://rangtuo.com/wp-content/plugins/myplugin/
注意结尾有反斜杠。
plugin_dir_path()
返回当前插件目录的服务器绝对路径,例如
echo plugin_dir_path(__FILE__ );
//输出:/home/user/public_html/wp-content/plugins/myplugin/


获取当前插件的目录的 URI,例如一个插件位于/wp-content/plugins/myplugin下,该目录下放有插件的主文件名为myplugin.php,在myplugin.php中执行下面的代码,结果如下:

 

plugin_basename()

返回调用该函数的插件文件名称(包含插件路径)

例如在插件myplugin下的myplugin.php文件中调用该函数,结果如下:

echo plugin_basename(__FILE__);
//输出:myplugin/myplugin.php
如果在myplugin/include/test.php文件中调用(test.php通过include引用到myplugin.php中),结果如下
echo plugin_basename(__FILE__);
//输出:myplugin/include/test.php

Url 路径相关常量

WordPress 中还有一组用define定义的常量代表路径。


WP_CONTENT_DIR

wp-content 目录的服务器绝对路径,例如 /home/user/public_html/wp-content


WP_CONTENT_URL

wp-content 目录的 URI 地址,例如 https://rangtuo.com/wp-content


WP_PLUGIN_DIR

插件目录的服务器绝对路径,例如 /home/user/public_html/wp-content/plugins


WP_PLUGIN_URL

插件目录的 URI 地址,例如 https://rangtuo.com/wp-content/plugins

声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
RESTful API 执行 delete 返回204无法获取 Body
Node.js 实现 RBAC 权限模型
WordPress 数据库操作
CSS3 实现 Switch 开关
CSS 滚动条样式修改
WordPress 一键从HTTP转换到HTTPS
WP_REST_Response 返回结果类
Node.js 使用 Jest 做单元测试