社区新闻

WordPress 块主题的品牌维护模式

查看官方原文 ↗ 发布于

当WordPress对核心、插件或主题进行更新时,会短暂进入维护模式。访客看到一条简洁、无样式的提示:“短暂无法进行定期维护。一会儿再来看看。”虽然有效,但距离品牌应有的精致体验还有很大差距。

对于方块主题,有一个干净的解决方案,可以合理地拆分作品。首先,来个主题的钩子。之后,维护页面完全由网站编辑器创建和管理。没有文件编辑,没有代码,只有块。维护模式可以根据你的工作流程自由开启和关闭。

钩子

将以下内容添加到你的主题中。这是一次性任务,一旦安装好,维护模式无需更改代码。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

维护模式模板正在为未登录用户主动加载。
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 )发送“服务不可用”的回复,让爬虫知道停机是暂时的,而非永久性变化。 建议他们一小时后再来看看。 防止响应被CDN或代理存储,因此一旦关闭维护模式,实时站点即可立即提供。这些都不影响访客所看到的内容。Retry-After: 3600nocache_headers()

注意,在这个版本中,头部是发送到模板检查内的,所以除非实际提供维护模板,否则它们不会被设置。

创建维护模板

有了挂钩后,维护页面完全在网站编辑器中创建。

进入外观→编辑器→模板,添加新模板。把它命名为维护,WordPress 会给它分配 slug,这正是 hook 想要的。maintenance

Twenty Twentyfour 区块主题中的维护模式模板。
在网站编辑器中创建维护模板,就像使用其他模板一样,使用图案、模板部分等。

像搭建其他模板一样。它完全访问了你主题的全局样式,所以你的字体、颜色和间距都可用。在完整网站框架中包含你的页眉和页脚模板部分,或者为了更聚焦的页面而省略它们。添加标题、信息、标志,任何能反映你品牌的东西。

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

激活和关闭维护模式

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

模板浏览器中的维护模式已停用。
关闭维护模式只需重命名模板即可。

测试

在网站编辑器中创建维护模板,然后登出访问网站。你应该看看你的维护页面。重新登录并确认在线站点是否正常加载。满意后,删除模板。

结论

维护页面虽然是个小细节,但访客会注意到。这里的做法是让工作留在原位:开发者只需添加一次钩子,它就会被排除在外,而负责网站内容和品牌的人则通过网站编辑器完全拥有维护页面。没有静态文件需要管理,没有代码需要更新,只有一个模板,需要时用,不需要时就消失。