WordPress Maintenance Mode Without a Plugin Part 3

Code,CoolStuff,HowTo,PHP,Snippet,WordPress 22 October 2009 13 Comments

A few months ago I wrote part 1 and part 2 of WordPress Maintenance Mode Without a Plugin. Part 1 covered the basics of using the .maintenance file, and part 2 covered styling the maintenance page using wp-content/maintenance.php. Part 3 covers the short comings of the other 2 by addressing how to let a user log into the admin and allowing logged in users access to the front end of the site while in maintenance mode.

It only takes a little bit of extra code in a file called .maintenance in the root of your WordPress installation to conditionally return a time that falls within the logic described in part 1. Now without forther adieu:

<?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();
else
    $upgrading = 0;
?>

Just drop the above code in the .maintenance file perhaps take a look at part 2 and away you go. Enjoy!

13 Responses on “WordPress Maintenance Mode Without a Plugin Part 3”

  1. Michał Żadkowski says:

    Hi, for some reason when I paste the last code snippet from part 3, I get errors about headers already sent, do you know maybe what am I doing wrong?

    • Matt says:

      Without knowing the exact message I can’t say exactly what the problem is. However I would imagine that it is related to having whites pace before the <?php or after the ?>. It is possible that the way the code was being displayed above could have caused extra white space. I have made a small modification. Copy the code again, making sure there is no white space as described and let me know if it works.

  2. Michał Żadkowski says:

    Works like a charm, thanks a zilion!

  3. winmonaye says:

    Easy enough and work great!! Thank you so much for sharing.

  4. I just thought I’d point out that this will work on WordPress 2.7+ (that’s when the support for the .maintenance file was added).

  5. Rosina Lippi says:

    I may well be missing something, but this would let anybody who is logged into the frontend. Is that right? Or can I put the weblog in maintenance mode and hide it from everybody but me (the only admin)?

    Thanks for your help.

    • Matt says:

      @Rosina: You are correct, anyone logged in would be able to access the site, not just admins. At the time when this maintenance stuff runs, none of the functions to check if the user is an admin are loaded. I plan on revisiting this post again sometime in the future to make a part 4. I am sure I could find a “hack” to get permissions levels to work.

  6. Dirk says:

    again excellent article, thanks so much

  7. Thanks for the tip Matt!

  8. Hikari says:

    I use Maintenance Mode plugin, which has some higher features, but I have a problem with it. I always forget to disable it, and ppl end up facing 503 message until some friend tells me :P

    With a small tweak on your code I can enter admin pages, and when I go to frontend I see it’s maintenance and relember to remove it. tnx a lot :D

  9. Vito Botta says:

    I prefer this to using a plugin. Quick & easy, thanks!

  10. Venkat says:

    Hi Matt,

    I’m reading Professional WordPress (Wrox Book). I came to know about .maintenance and maintenance.php from that book. I was wondering whether there is a way to access backend/frontend while in maintenance mode. Got it here. Thanks man!

  11. Abhishek says:

    Excellent! Exactly what I was looking for. :)

Leave a Reply