站点路径相关函数
home_url()
返回站点路径,相当于后台设置->常规中的”站点地址(URL)”。
$url = home_url();
echo $url;
// 输出: https://www.seo628.com
$url = home_url('/images/');
echo $url;
// 输出:https://www.seo628.com/images/
site_url()
如果WordPress安装在域名根目录下,则该函数与home_url()相同。
如果WordPress安装在子目录下,例如https://www.seo628.com/blog/,则site_url()返回WordPress实际安装地址,相当于后台->设置->常规中的”WordPress地址(URL)”。
$url = site_url();
echo $url;
// 假设WordPress安装在https://www.seo628.com/blog/下
// 输出:https://www.seo628.com/blog/
admin_url()
返回后台地址,传递参数后也可返回后台menu的地址。
$url = admin_url();
echo $url;
// 输出:https://www.seo628.com/wp-admin/
$url = admin_url('post-new.php');
echo $url;
// 输出:https://www.seo628.com/wp-admin/post-new.php
content_url()
返回实际的wp-content目录URI。
$url = content_url();
echo $url;
// 输出:https://www.seo628.com/wp-content
includes_url()
返回当前WordPress站点存放核心文件的目录wp-includes的地址,可以带一个$path作为参数。
$url = includes_url('/js/');
echo $url;
// 输出:https://www.seo628.com/wp-includes/js/
wp_upload_dir()
返回WordPress上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。
$upload_dir = wp_upload_dir();
数组包含以下信息:
path– 上传目录的服务器绝对路径,通常以反斜杠(/)开头url– 上传目录的完整URLsubdir– 子目录名称,通常是以年/月形式组织的目录地址,例如/2026/02basedir– 上传目录的服务器绝对路径,不包含子目录baseurl– 上传目录的完整URL,不包含子目录error– 报错信息
$upload_dir = wp_upload_dir();
echo $upload_dir['baseurl'];
// 输出:https://www.seo628.com/wp-content/uploads
echo $upload_dir['path'];
// 输出:/home/user/public_html/wp-content/uploads/2026/02
主题路径相关函数
get_theme_root_uri()
获取存放主题的目录URI。
echo get_theme_root_uri();
// 输出:https://www.seo628.com/wp-content/themes
get_theme_root()
获取存放主题的目录的服务器绝对路径。
echo get_theme_root();
// 输出:/home/user/public_html/wp-content/themes
get_theme_roots()
获取主题目录的目录名称。
echo get_theme_roots();
// 输出:/themes
get_stylesheet_directory()
获取当前启用的主题目录的服务器绝对路径。
echo get_stylesheet_directory();
// 输出:/home/user/public_html/wp-content/themes/twentytwenty
// 可以用来include文件
include(get_stylesheet_directory() . '/includes/myfile.php');
get_stylesheet_directory_uri()
获取当前启用的主题目录的URI。
echo get_stylesheet_directory_uri();
// 输出:https://www.seo628.com/wp-content/themes/twentytwenty
get_template_directory()
获取父主题目录的服务器绝对路径(适用于子主题)。
echo get_template_directory();
// 输出:/home/user/public_html/wp-content/themes/parent-theme
get_template_directory_uri()
获取父主题目录的URI(适用于子主题)。
echo get_template_directory_uri();
// 输出:https://www.seo628.com/wp-content/themes/parent-theme
get_stylesheet()
获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。
echo get_stylesheet();
// 输出:child-theme
插件路径相关函数
plugins_url()
获取插件的目录URI。
// 在插件主文件中(如:/wp-content/plugins/myplugin/myplugin.php)
echo plugins_url();
// 输出:https://www.seo628.com/wp-content/plugins
echo plugins_url('', __FILE__);
// 输出:https://www.seo628.com/wp-content/plugins/myplugin
echo plugins_url('js/myscript.js', __FILE__);
// 输出:https://www.seo628.com/wp-content/plugins/myplugin/js/myscript.js
plugin_dir_url()
返回当前插件的目录URI。
echo plugin_dir_url(__FILE__);
// 输出:https://www.seo628.com/wp-content/plugins/myplugin/
// 注意结尾有反斜杠
plugin_dir_path()
返回当前插件目录的服务器绝对路径。
echo plugin_dir_path(__FILE__);
// 输出:/home/user/public_html/wp-content/plugins/myplugin/
// 使用示例
define('MYPLUGINNAME_PATH', plugin_dir_path(__FILE__));
require MYPLUGINNAME_PATH . 'includes/class-metabox.php';
require MYPLUGINNAME_PATH . 'includes/class-widget.php';
plugin_basename()
返回调用该函数的插件文件名称(包含插件路径)。
// 在插件myplugin下的myplugin.php文件中调用
echo plugin_basename(__FILE__);
// 输出:myplugin/myplugin.php
// 在myplugin/include/test.php文件中调用
echo plugin_basename(__FILE__);
// 输出:myplugin/include/test.php
其他常用函数
get_permalink()
获取文章/页面的固定链接。
echo get_permalink($post_id);
get_the_post_thumbnail_url()
获取文章特色图片的URL。
echo get_the_post_thumbnail_url($post_id, 'full');
get_avatar_url()
获取用户头像的URL。
echo get_avatar_url($user_id, ['size' => 96]);
URL路径相关常量
WordPress中还有一组用define定义的常量代表路径。
服务器绝对路径常量
// WP核心目录
ABSPATH
// 示例:/home/user/public_html/
// wp-content目录
WP_CONTENT_DIR
// 示例:/home/user/public_html/wp-content
// 插件目录
WP_PLUGIN_DIR
// 示例:/home/user/public_html/wp-content/plugins
// 上传目录
UPLOADS
// 示例:/home/user/public_html/wp-content/uploads
URL地址常量
// 站点URL
home_url() 对应常量:无直接常量,但可通过 get_option('home') 获取
// WordPress安装URL
site_url() 对应常量:无直接常量
// wp-content目录URL
WP_CONTENT_URL
// 示例:https://www.seo628.com/wp-content
// 插件目录URL
WP_PLUGIN_URL
// 示例:https://www.seo628.com/wp-content/plugins
// 管理员邮件
admin_url('admin-ajax.php')
// 用于Ajax请求
使用建议
- 优先使用函数而非常量:函数更灵活,能够适应各种安装环境
- 主题开发中:优先使用
get_stylesheet_directory_uri()和get_template_directory_uri() - 插件开发中:优先使用
plugins_url()和plugin_dir_path() - 多站点环境:使用
network_home_url()和network_site_url()获取网络级别的URL - 安全考虑:输出URL时使用
esc_url()进行转义
常见用例示例
// 引入主题样式文件
wp_enqueue_style('theme-style', get_stylesheet_directory_uri() . '/css/style.css');
// 引入插件脚本文件
wp_enqueue_script('plugin-script', plugins_url('js/script.js', __FILE__));
// 获取文章特色图片
$featured_image = get_the_post_thumbnail_url(get_the_ID(), 'full');
// 在主题文件中包含其他文件
include get_template_directory() . '/inc/custom-functions.php';
// 在插件中定义路径常量
define('MY_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('MY_PLUGIN_URL', plugin_dir_url(__FILE__));
这些函数和常量是WordPress开发中最常用的路径和URL调用方法,合理使用它们可以使您的代码更加健壮和可移植。


湘公网安备43020002000238