wp_maintenance()WP 3.0.0

Die with a maintenance message when conditions are met.

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

No Hooks.

Return

null. Nothing.

Usage

wp_maintenance();

Examples

0

#1 Putting the site into maintenance mode (updates, development)

When updating code on the site, it is recommended to put the site in maintenance mode (block for the duration of the update), so that sudden visits from users could not affect the process of updating files or DB.

To enable such a mode you need:

Option 1:

Create file .maintenance in the root of the site (directory where wp-admin folder is located) with the following code:

<?php
// UNIX time stamp after which + 10 minutes,
// the site will exit development mode.
$upgrading = 1652995051; // time();

Option 2:

Create an empty file ABSPATH . '.maintenance' and manually at an early stage, for example in the wp-config.php file specify a global variable $upgrading:

// UNIX time stamp after which + 10 minutes,
// the site will exit development mode.
$GLOBALS['upgrading'] = 1652995051; // time();

Done!

Refresh the page and see the maintenance message:

The site is briefly closed for maintenance. Come back in a minute.

0

#2 Let's change the text of the maintenance message:

Create a file /wp-content/maintenance.php with following code:

<?php
wp_load_translations_early();

$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ){
	$protocol = 'HTTP/1.0';
}
header( "$protocol 503 Service Unavailable", true, 503 );
header( 'Content-Type: text/html; charset=utf-8' );
header( 'Retry-After: 600' );

// most WP functions do not work
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Maintenance</title>
</head>
<body>
	<div style="max-width:800px; margin:50px auto; text-align:center;">
		<h1>Site is under maintenance. The correct work will be restored in a few minutes.</h1>
		<h2>Sorry for the inconvenience.</h2>
	</div>
</body>
</html>

When the site is updated, our message about the inaccessibility of the site will be changed and we will see the text specified above.

Changelog

Since 3.0.0 Introduced.

Code of wp_maintenance() WP 5.9.3

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
	);
}