函数文档

wp_maintenance()

💡 云策文档标注

概述

wp_maintenance() 函数在满足条件时终止 WordPress 执行并显示维护消息。它支持通过自定义 maintenance.php 文件替换默认消息。

关键要点

  • 函数首先检查 wp_is_maintenance_mode() 是否启用维护模式,未启用则直接返回。
  • 如果存在 wp-content/maintenance.php 文件,则加载该文件并终止执行。
  • 否则,加载核心函数和翻译,设置 Retry-After 头,并调用 wp_die() 显示默认维护消息。
  • 默认消息可通过 __() 函数翻译,状态码为 503。

代码示例

function wp_maintenance() {
	// Return if maintenance mode is disabled.
	if ( ! wp_is_maintenance_mode() ) {
		return;
	}

	if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
		require_once WP_CONTENT_DIR . '/maintenance.php';
		die();
	}

	require_once ABSPATH . WPINC . '/functions.php';
	wp_load_translations_early();

	header( 'Retry-After: 600' );

	wp_die(
		__( 'Briefly unavailable for scheduled maintenance. Check back in a minute.' ),
		__( 'Maintenance' ),
		503
	);
}

注意事项

  • 自定义 maintenance.php 文件应放置在 wp-content 目录下,用于覆盖默认维护页面。
  • 函数自 WordPress 3.0.0 版本引入,相关函数包括 wp_is_maintenance_mode()、wp_load_translations_early()、__() 和 wp_die()。

📄 原文内容

Dies with a maintenance message when conditions are met.

Description

The default message can be replaced by using a drop-in (maintenance.php in the wp-content directory).

Source

function wp_maintenance() {
	// Return if maintenance mode is disabled.
	if ( ! wp_is_maintenance_mode() ) {
		return;
	}

	if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
		require_once WP_CONTENT_DIR . '/maintenance.php';
		die();
	}

	require_once ABSPATH . WPINC . '/functions.php';
	wp_load_translations_early();

	header( 'Retry-After: 600' );

	wp_die(
		__( 'Briefly unavailable for scheduled maintenance. Check back in a minute.' ),
		__( 'Maintenance' ),
		503
	);
}

Changelog

Version Description
3.0.0 Introduced.