WordPress Maintenance Mode

WordPress has had maintenance mode functionality built into its core since WordPress core updates were added. You may never have noticed, since a core update generally only takes a couple of seconds, but your WordPress installation is put into “maintenance mode” whilst the update is installing.

Hooking into this functionality is really quite simple and effective, and understanding how it works is essential for any WordPress webmaster.

There are several ways of putting your website into Maintenance Mode:

WordPress .maintenance File

When running a core update, a file called .maintenance is created in the root of your WordPress installation. Note the preceding dot like a .htaccess file; in Unix/Linux, this is considered a hidden file. In this file is the following code:

<?php $upgrading = time(); ?>

This code causes the maintenance page to display until the .maintenance file is removed. There are two checks in wp-settings.php to see if the maintenance page should be displayed. First, the .maintenance file should exist. Second, it checks that the current time minus the time specified by the $upgrading variable is less than 10 minutes. The code above ensures that it will always be in maintenance mode because the function time(); gives the current time, and time() - time() == 0.

WordPress Maintenance Mode Function

If you want to do some more significant updates to your WordPress installation – for example, testing out a custom plugin or editing your theme – this is a better solution. Adding the following code into your functions.php file (which you can find from Appearance → Editor) it will display the maintenance message for everyone not logged in, but still allow you, as an Administrator, to view the site:

function wpr_maintenance_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('Maintenance, please come back soon.', 'Your Website Name'));
    }
}
add_action('get_header', 'wpr_maintenance_mode');

Maintenance Mode Plugins

Of course, if you’re not confident writing code or uploading files, there are a multitude of plugins you can install which do the same thing. Here are some of the more popular plugins:

Stuck in Maintenance Mode?

On the rare occasion, a core update will not run successfully, and when this happens, you may find yourself stuck in maintenance mode. The solution is simple enough. When the core update began, a .maintenance file was created in the root of your WordPress installation. Just log in with FTP, or open the File Manager in cPanel, and delete this file.

Formatting the Maintenance Page

After the wp-settings.php file determines whether or not to put the blog into maintenance mode, it checks to see if there is a file called maintenance.php located in WP_CONTENT_DIR which is by default wp-content/.

Simply create a file at wp-content/maintenance.php containing the code you want to display the for the maintenance page. Below is a sample of code based off of the default maintenance page.

<?php
$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' );
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Maintenance</title>
</head>

<body>
    <h1>Briefly unavailable for scheduled maintenance. Check back in a minute.</h1>
</body>
</html>

<?php die(); ?>

And finally…

For the coders among you, the code from the Maintenance Mode function can be inserted into the .maintenance file, allowing you as an administrator to go about the business of running the site, while visits are redirected to the Maintenance Mode message. Here’s how:

<?php
function is_user_logged_in() {
    $loggedin = false;
    foreach ( (array) $_COOKIE as $cookie => $value ) {
        if ( stristr($cookie, 'wordpress_logged_in_') )
            $loggedin = true;
    }
    return $loggedin;
}

if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin') && ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php') && ! is_user_logged_in() )
    $upgrading = time();
?>

Leave a Comment