Maintenance mode without a plugin

Just a simple way to put your blog into maintenance mode without a plugin. Nothing more to say!

This is a simple way to put your blog into maintenance mode without a plugin.

To achieve this, just place the following code in functions.php
NOTE: if you are using a child theme, place it in child theme’s functions.php

function wcr_maintain() {
    // we show this message if
    // 1. you're not logged in
    // 2. you're not on /wp-admin/ (index.php is redirecting you to wp-login.php)
    // 3. you're not on login page
    if (
         !is_user_logged_in() && 
         !is_admin() && 
         !in_array($GLOBALS['pagenow'], array('wp-login.php'))
       ) {
        $period = 3 * HOUR_IN_SECONDS; // 3 hours, but you can change if you need

        header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable', true, 503);
        header('Retry-After: ' . $period);

        // you can insert whatever you want :)
        echo 'content goes here';
        exit;
    }
}

add_action('init', 'wcr_maintain');

One comment

Leave a Reply

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