Maintenance mode without a plugin

Maintenance mode without a plugin

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';

add_action('init', 'wcr_maintain');
  • Tommy

    Thank you! Works awesome (used the code snippet plugin even).

Leave a Reply

Get our Wordpress
freebies to your inbox