WordPress: Maintenance mode without a plugin and customized maintenance page

I know there are plugins for maintenance mode. I found a 3-part tutorial that shows how easy this can be done with just 2 files. This post simply documents how this can be achieved for my own reference and is based on this tutorial.

The 2 files required:

  • .maintenance – to turn on maintenance mode
  • wp-content/maitnenance.php – customized maintenance page

Note that these files do not come with wp distribution and need to be created.

.maintenance

This file contains php code to turn on maintenance mode and triggers maintenance page to be displayed.

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

The above code will display the maintenance page on both front and back end.

To display maintenance page for non-login user,

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

When not required, rename the file to something else, say .maintenance.php.

wp-content/maintenance.php

This is the customized maintanance 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>Unavailable</title>
    </head>
    <body>
        <h1>This site is currently unavailable. Please come back later.</h1>
    </body>
</html>
<?php die(); ?>

Note codes line 1 to 6 and line 19.

Leave a Reply

Your email address will not be published. Required fields are marked *