目录结构
了解 WordPress 的目录结构对于主题和插件开发至关重要。
📁 WordPress 核心目录结构
wordpress/
├── wp-admin/ # 管理后台
│ ├── css/
│ ├── images/
│ ├── includes/
│ ├── js/
│ ├── network/ # 多站点网络管理
│ ├── user/ # 用户管理
│ └── admin.php # 管理核心
│
├── wp-content/ # 用户内容目录
│ ├── plugins/ # 插件目录
│ ├── themes/ # 主题目录
│ ├── uploads/ # 上传文件
│ ├── upgrade/ # 升级临时目录
│ ├── cache/ # 缓存文件
│ └── languages/ # 语言包
│
├── wp-includes/ # 核心类库
│ ├── css/
│ ├── fonts/
│ ├── images/
│ ├── js/
│ ├── query.php # WP_Query 类
│ ├── post.php # 文章相关函数
│ ├── user.php # 用户相关函数
│ ├── plugin.php # 插件基础
│ ├── theme.php # 主题基础
│ ├── post-type.php # 文章类型
│ ├── taxonomy.php # 分类法
│ ├── class-*.php # 各种类
│ └── functions.php # 核心函数
│
├── wp-config.php # 配置文件
├── wp-settings.php # 核心加载文件
├── wp-load.php # 加载文件
├── wp-blog-header.php # 博客头部
├── index.php # 主入口
└── xmlrpc.php # XML-RPC 接口1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
📂 主题目录结构
themes/
└── my-theme/
├── style.css # 主题样式(必需)
├── index.php # 主模板(必需)
├── functions.php # 主题函数
├── screenshot.png # 主题截图 (1200×900)
│
├── header.php # 页头模板
├── footer.php # 页脚模板
├── sidebar.php # 侧边栏模板
├── comments.php # 评论模板
│
├── front-page.php # 首页模板
├── home.php # 博客首页
├── single.php # 单篇文章
├── page.php # 单个页面
├── singular.php # 单一内容回退
├── archive.php # 归档页
├── author.php # 作者页
├── category.php # 分类页
├── tag.php # 标签页
├── taxonomy.php # 自定义分类页
├── search.php # 搜索结果
├── date.php # 日期归档
├── attachment.php # 附件页
├── image.php # 图片附件
├── 404.php # 404页面
│
├── template-parts/ # 模板部件
│ ├── content.php
│ ├── content-single.php
│ └── content-none.php
│
├── template-custom/ # 自定义模板
│ └── page-fullwidth.php
│
├── css/ # 样式目录
│ ├── style.css
│ ├── editor-style.css
│ └── rtl.css
│
├── js/ # 脚本目录
│ ├── navigation.js
│ └── skip-link-focus-fix.js
│
├── inc/ # 包含文件
│ ├── custom-header.php
│ ├── template-tags.php
│ └── customizer.php
│
├── languages/ # 翻译文件
│ ├──.pot # 翻译模板
│ └── zh_CN.po # 中文翻译
│
└── assets/ # 静态资源
├── images/
└── fonts/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
📂 插件目录结构
plugins/
└── my-plugin/
├── my-plugin.php # 主插件文件(必需)
├── uninstall.php # 卸载处理(可选)
├── readme.txt # WordPress.org 说明
├── index.php # 安全文件
│
├── admin/ # 管理界面
│ ├── index.php
│ ├── css/
│ ├── js/
│ ├── images/
│ └── partials/ # 管理模板
│ └── settings-view.php
│
├── public/ # 公共界面
│ ├── index.php
│ ├── css/
│ ├── js/
│ └── partials/
│ └── public-display.php
│
├── includes/ # 核心类
│ ├── index.php
│ ├── class-my-plugin.php
│ ├── class-my-plugin-loader.php
│ ├── class-my-plugin-admin.php
│ └── class-my-plugin-public.php
│
├── languages/ # 国际化
│ ├── index.php
│ └── my-plugin.pot
│
└── assets/ # 资源文件
├── images/
├── screenshots/
│ └── screenshot-1.png
└── fonts/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
🔧 常用目录常量
| 常量 | 默认值 | 说明 |
|---|---|---|
WP_CONTENT_DIR | /wp-content | 内容目录路径 |
WP_CONTENT_URL | /wp-content | 内容目录 URL |
WP_PLUGIN_DIR | /wp-content/plugins | 插件目录路径 |
WP_PLUGIN_URL | /wp-content/plugins | 插件目录 URL |
WP_LANG_DIR | /wp-content/languages | 语言目录 |
WP_UPLOAD_DIR | /wp-content/uploads | 上传目录 |
TEMPLATEPATH | 主题路径 | 主题路径(已废弃) |
STYLESHEETPATH | 样式表路径 | 子主题路径 |
📝 路径和 URL 获取
php
<?php
// 获取路径
get_theme_file_path('template-parts/header.php');
get_parent_theme_file_path('style.css');
get_stylesheet_directory(); // 子主题路径
get_template_directory(); // 父主题路径
// 获取 URL
get_theme_file_uri('js/main.js');
get_parent_theme_file_uri('style.css');
get_stylesheet_directory_uri(); // 子主题 URL
get_template_directory_uri(); // 父主题 URL
// 插件路径
plugin_dir_path(__FILE__);
plugin_dir_url(__FILE__);
plugin_basename(__FILE__);
// 内容目录
WP_CONTENT_DIR;
WP_CONTENT_URL;
// 上传目录
wp_upload_dir(); // 返回数组
// Array(
// [path] => /var/www/html/wp-content/uploads/2024/01
// [url] => https://example.com/wp-content/uploads/2024/01
// [subdir] => /2024/01
// [basedir] => /var/www/html/wp-content/uploads
// [baseurl] => https://example.com/wp-content/uploads
// [error] =>
// )1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
🛡️ 安全最佳实践
php
<?php
// 防止直接访问插件文件
if (!defined('ABSPATH')) {
exit;
}
// 防止直接访问目录
// 在每个目录添加 index.php
<?php
// Silence is golden.1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
