WordPress获取网站根目录、主题目录、插件目录路径和url地址

本文介绍了WordPress中常用的路径和URL调用函数,包括站点根目录、主题目录和插件目录等。主要函数有home_url()返回站点地址,site_url()返回WordPress安装地址,admin_url()指向后台,content_url()获取wp-content目录,以及get_stylesheet_directory_uri()和plugins_url()分别用于主题和插件的路径获取。这些函数帮助开发者动态调用路径,提升开发效率。

WordPress基础教程
阅读时间: 60 分钟
最后更新时间:2026年3月2日

站点路径相关函数

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– 上传目录的完整URL
  • subdir– 子目录名称,通常是以年/月形式组织的目录地址,例如/2026/02
  • basedir– 上传目录的服务器绝对路径,不包含子目录
  • 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请求

使用建议

  1. 优先使用函数而非常量:函数更灵活,能够适应各种安装环境
  2. 主题开发中:优先使用get_stylesheet_directory_uri()get_template_directory_uri()
  3. 插件开发中:优先使用plugins_url()plugin_dir_path()
  4. 多站点环境:使用network_home_url()network_site_url()获取网络级别的URL
  5. 安全考虑:输出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调用方法,合理使用它们可以使您的代码更加健壮和可移植。

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

在AI里面继续讨论:

曾凤祥

曾凤祥

WordPress技术负责人
小兽WordPress凭借15年的WordPress企业网站开发经验,坚持以“为企业而生的WordPress服务”为宗旨,累计为10万多家客户提供高品质WordPress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽WordPress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部