wordpress: maintenance mode without a plugin and customized maintenance page

by prettyscripts on 2012-01-16 14:54

freewordpress

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:

<?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,

PHP:

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

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

wp-content/maintenance.php

this is the customized maintanance page.

PHP:

<?php
$protocol $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
        $protocol 'HTTP/1.0';
header"$protocol 503 Service Unavailable"true503 );
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.