跳到正文
原文
WordPress 开发者博客· Troy Chaplin·· 2026-07-13编辑评分28

WordPress block themes 品牌化维护模式指南

On-brand maintenance mode for WordPress Block Themes

编辑导读

WordPress block themes 可通过 theme 钩子使用 Site Editor 创建品牌化维护页面,无需编辑文件或编写代码。登录用户会绕过维护页面;SEO 方案使用 status_header( 503 )、Retry-After: 3600 和 nocache_headers 临时停站并避免缓存。维护模板命名为 Maintenance 后自动启用,重命名或删除即可停用。

当 WordPress 对核心、插件或主题运行更新时,会短暂进入维护模式。访客看到的是一条朴素的、未经样式化的消息:“计划维护期间暂时无法访问,请稍后再试。” 它确实有效,但与你的品牌应有的精致体验相去甚远。

对于区块主题,有一个简洁的解决方案,能够合理拆分各项工作。首先,在主题中添加一个钩子。此后,维护页面将完全在站点编辑器中创建和管理。无需编辑文件,无需编写代码,只需使用区块。维护模式可以根据最适合你的工作流的方式启用和停用。

目录

  1. The hook
    1. 最小化钩子方案
    2. SEO 友好的响应头方案
  2. 创建维护模板
  3. 启用和停用维护模式
  4. 测试
  5. 结论

钩子

将以下内容添加到主题的 functions.php 中。这是一项一次性任务;设置完成后,管理维护模式无需再更改代码。

最小化钩子方案

此方案对于大多数在常规更新时段进行更新的网站都已完全够用:

/**
* Serve the template titled "Maintenance" if the template exists.
*
* @param string $template The path to the template.
* @return string The maintenance template path, or the original template.
*/
function your_theme_force_maintenance_template( $template ) {
   if ( is_user_logged_in() ) {
       return $template;
   }

   if ( ! current_theme_supports( 'block-templates' ) ) {
       return $template;
   }

   // Look for template(s) titled exactly "Maintenance".
   $maintenance_posts = get_posts(
       array(
           'post_type'      => 'wp_template',
           'title'          => 'Maintenance',
           'post_status'    => array( 'publish', 'draft' ),
           'posts_per_page' => -1,
           'no_found_rows'  => true,
       )
   );

   $maintenance_post = null;

   foreach ( $maintenance_posts as $post ) {
       $theme_slugs = wp_get_post_terms( $post->ID, 'wp_theme', array( 'fields' => 'names' ) );
       if ( in_array( get_stylesheet(), $theme_slugs, true ) ) {
           $maintenance_post = $post;
           break;
       }
   }

   if ( ! $maintenance_post ) {
       return $template;
   }

   $slug = $maintenance_post->post_name;

   return locate_block_template( $template, $slug, array( $slug ) );
}
add_filter( 'template_include', 'your_theme_force_maintenance_template', 99 );

locate_block_template() 会按正确的优先级检查维护模板,并优先检查数据库。这意味着,只要模板名称为 maintenance,在站点编辑器中创建的模板就会被找到。任何已登录用户都会直接绕过维护模式,因此你的团队可以在更新期间查看线上网站,而不会进入维护页面。

Maintenance mode template actively loading for non-logged in users.
在 Twenty Twentyfour 中创建的自定义维护模式模板示例。

SEO 友好的响应头方案

如果你的网站拥有可观的搜索流量,或维护时段往往持续较长时间,那么值得添加三个响应头,向搜索引擎爬虫说明停机只是暂时的:

/**
* Serve the template titled "Maintenance" if the template exists.
* Includes headers to signal temporary unavailability to search engines.
*
* @param string $template The path to the template.
* @return string The maintenance template path, or the original template.
*/
function your_theme_force_maintenance_template( $template ) {
   if ( is_user_logged_in() ) {
       return $template;
   }

   if ( ! current_theme_supports( 'block-templates' ) ) {
       return $template;
   }

   // Look for template(s) titled exactly "Maintenance".
   $maintenance_posts = get_posts(
       array(
           'post_type'      => 'wp_template',
           'title'          => 'Maintenance',
           'post_status'    => array( 'publish', 'draft' ),
           'posts_per_page' => -1,
           'no_found_rows'  => true,
       )
   );

   $maintenance_post = null;

   foreach ( $maintenance_posts as $post ) {
       $theme_slugs = wp_get_post_terms( $post->ID, 'wp_theme', array( 'fields' => 'names' ) );
       if ( in_array( get_stylesheet(), $theme_slugs, true ) ) {
           $maintenance_post = $post;
           break;
       }
   }

   if ( ! $maintenance_post ) {
       return $template;
   }

   $slug = $maintenance_post->post_name;

   $maintenance_template = locate_block_template( $template, $slug, array( $slug ) );

   if ( ! empty( $maintenance_template ) ) {
       nocache_headers();
       status_header( 503 );
       header( 'Retry-After: 3600' );
       return $maintenance_template;
   }

   return $template;
}
add_filter( 'template_include', 'your_theme_force_maintenance_template', 99 );

status_header( 503 ) 会返回“Service Unavailable”响应,让爬虫知道停机是暂时的,而非永久性变更。Retry-After: 3600 建议它们一小时后再来检查。nocache_headers() 可防止响应被 CDN 或代理服务器缓存,因此停用维护模式后会立即提供线上网站。以上设置均不会影响访客看到的内容。

请注意,在这个版本中,响应头是在检查模板时发送的,因此只有在实际提供维护模板时才会设置这些响应头。

创建维护模板

钩子设置完成后,维护页面将完全在站点编辑器中创建。

前往 外观 → 编辑器 → 模板,添加一个新模板。将其命名为 Maintenance,WordPress 会为其分配别名 maintenance,这正是钩子要查找的名称。

A maintenance mode template in the Twenty Twentyfour block theme.
像创建其他任何模板一样,在站点编辑器中创建维护模板,可以使用图案、模板部件等。

按照构建其他模板的方式完成它。它可以完全使用主题的全局样式,因此你可以使用所有字体、颜色和间距设置。加入页眉和页脚模板部件以呈现完整的网站框架,也可以省略它们,以打造更聚焦的页面。添加标题、消息、标志,或任何能体现你的品牌的内容。

保存后,模板将存储在数据库中,钩子会自动找到它。

启用和停用维护模式

最快的切换方式是直接重命名模板本身。由于钩子会检查名为 maintenance 的模板,创建该模板(如上所述)就会开启维护模式。要将其关闭,请前往 外观 → 编辑器 → 模板,找到 Maintenance 模板,然后重命名或删除它——无需访问文件,也无需代码或配置。

Deactivated maintenance mode in the template browser.
停用维护模式只需重命名模板。

测试

在站点编辑器中创建“维护”模板,然后退出登录并访问站点。你应该会看到自己的维护页面。重新登录并确认线上站点正常加载。满意后,删除该模板。

总结

维护页面只是个小细节,却是访客会注意到的。这里的做法让工作各归其位:开发者只需添加一次钩子,此后便无需操心;而负责站点内容和品牌的人员则可以完全通过站点编辑器掌控维护页面。无需管理静态文件,消息需要更新时也不用改动代码,只需一个随需存在、随需删除的模板。

特别感谢 @sumitsingh 和 @bph 审阅本文并提供反馈。

来源:WordPress 开发者博客 · https://developer.wordpress.org/news/2026/07/on-brand-maintenance-mode-for-wordpress-block-themes/

部分链接为推广/关联链接。