启用 blockTemplates 后,如何为 404 页面提供全站编辑模板?
解读
在国内企业落地“全站编辑(FSE)”项目时,面试官常通过“404 模板怎么接入 blockTemplates”来验证候选人是否真正理解 Block Theme 的模板层级、模板查询顺序、theme.json 声明机制 以及 WordPress 6+ 的模板回退规则。很多开发者只会在 /templates/404.html 里写死区块,结果导致:
- 用户后台“模板”列表里找不到 404,无法可视化编辑;
- 切换 blockTemplates 后,前端依旧读取 PHP 文件,FSE 不生效;
- 多语言插件(WPML、Polylang)无法为不同语言绑定独立 404 模板。
因此,答案必须覆盖 “让 WordPress 认识模板 + 让用户能编辑 + 让前端正确回退” 三个闭环。
知识点
- blockTemplates 启用标志:
add_theme_support( 'block-templates' )或直接在 block theme 的theme.json里声明"version": 2。 - 模板查询优先级:
slug-404.html>404.html>index.html;当block-templates启用时,内核优先在wp_templateCPT 里查找,再回退到主题目录。 - 模板注册 API:
register_block_template()(WP 6.4+)或block_template_part_{$part}过滤器;传统theme_templates过滤器已废弃。 - 模板锁定与可编辑性:
templateLock属性、wp_templatepost type 的theme字段必须与当前主题 stylesheet 一致,否则后台不展示。 - 多语言兼容:WPML 6.2+ 通过
wpml_current_language钩子给模板 slug 加后缀,如404-zh-hans,需在注册时动态拼接。
答案
分四步落地,代码直接贴进 functions.php 或插件均可通过面试追问。
-
声明支持并保证目录结构
在主题根目录放templates/404.html作为默认回退文件,内容示例:<!-- wp:template-part {"slug":"header","tagName":"header"} /--> <!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} --> <main class="wp-block-group"> <!-- wp:heading {"level":1} --> <h1>404</h1> <!-- /wp:heading --> <!-- wp:paragraph --> <p>抱歉,您访问的页面不存在</p> <!-- /wp:paragraph --> </main> <!-- /wp:group --> <!-- wp:template-part {"slug":"footer","tagName":"footer"} /--> -
注册模板到数据库,确保后台可见
使用 WP 6.4 提供的register_block_template(国内主机已普遍升级 6.5,可放心用):add_action( 'init', function () { register_block_template( 'twentyfourfour/404', // 命名空间/模板 slug [ 'title' => '404 模板', 'description' => '全站编辑的 404 页面模板', 'content' => file_get_contents( get_theme_file_path( 'templates/404.html' ) ), 'post_types' => [], // 空数组代表不绑定任何文章类型 ] ); } );若主机仍在 6.3 以下,可退而求其次:
add_filter( 'get_block_templates', function ( $templates, $query, $template_type ) { if ( $template_type !== 'wp_template' ) { return $templates; } if ( ! empty( $query['slug__in'] ) && ! in_array( '404', $query['slug__in'], true ) ) { return $templates; } $templates[] = new WP_Block_Template( (object) [ 'slug' => '404', 'id' => 'theme//404', 'theme' => get_stylesheet(), 'type' => 'wp_template', 'title' => '404 模板', 'description' => '全站编辑的 404 页面', 'content' => file_get_contents( get_theme_file_path( 'templates/404.html' ) ), 'source' => 'theme', // 必须,否则后台不展示 'is_custom' => false, ] ); return $templates; }, 10, 3 ); -
保证前端优先走数据库模板
当block-templates启用时,WordPress 会自动在wp_template表里找 slug=404 且主题字段匹配的记录,无需额外钩子;但若主题曾通过 PHP 文件404.php提供兜底,需要显式移除:add_filter( 'template_include', function ( $template ) { if ( is_404() && locate_block_template( '404', 'wp_template' ) ) { return locate_block_template( '404', 'wp_template' ); // 返回数据库模板路径 } return $template; }, 99 ); -
多语言站点多模板支持(加分项)
在 WPML 场景下,为不同语言注册独立模板:add_action( 'init', function () { $langs = apply_filters( 'wpml_active_languages', [] ); foreach ( $langs as $code => $lang ) { register_block_template( 'twentyfourfour/404-' . $code, [ 'title' => "404 模板 ($code)", 'description' => "语言 $code 的 404 页面", 'content' => file_get_contents( get_theme_file_path( "templates/404-{$code}.html" ) ), ] ); } } );前端根据
wpml_current_language动态切换:add_filter( 'template_redirect', function () { if ( is_404() ) { $lang = apply_filters( 'wpml_current_language', null ); $tpl = 'twentyfourfour/404-' . $lang; if ( get_block_template( $tpl, 'wp_template' ) ) { add_filter( 'pre_get_block_file_template', fn() => get_block_template( $tpl, 'wp_template' ), 10, 1 ); } } } );
完成以上四步,用户在后台“外观 > 编辑器 > 模板”里即可看到“404 模板”,可拖拽任意区块;前端 404 请求优先读取数据库里的区块模板,实现真正的全站编辑。
拓展思考
- 性能:
register_block_template会在每次init把模板内容写进wp_template表,高并发场景建议加wp_cache_get/set做内存缓存,或改用theme//404的 file 模板,减少数据库 IO。 - 安全:若允许客户后台编辑,务必通过
current_user_can( 'edit_theme_options' )做权限校验,防止低权限用户篡改 404 模板植入恶意区块。 - 回退策略:当
wp_template表被误删,如何让站点立刻回到文件模板?可在template_include过滤器里加! get_block_template( '404', 'wp_template' )的判断,自动重定向到get_theme_file_path( 'templates/404.html' ),保障高可用。 - CI/CD:国内团队普遍用 GitLab Runner 或阿里云效做自动部署,记得在
.gitlab-ci.yml里加一步wp eval 'register_block_template(...)',确保新模板随版本一起写库,避免人工点后台“导入模板”。 - 面试追问:如果面试官继续问“如何把 404 模板拆成可复用的模板部件(template part)”,可答:将公共结构抽成
template-parts/not-found.html,在 404 模板里用<!-- wp:template-part {"slug":"not-found","theme":"twentyfourfour"} /-->引用,并在theme.json里声明"templateParts": [ { "name": "not-found", "area": "uncategorized" } ],实现一处修改、全站生效。