WordPress Maintenance Mode Without a Plugin Part 3

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

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

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in Code, CoolStuff, HowTo, PHP, Snippet, WordPress. Bookmark the permalink.

18 Responses to 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. :)

  12. Silwing says:

    Great tip! Just what I needed.

  13. Andreas Lorentzen says:

    If you delete the 2 below lines from the example:
    else
    $upgrading = 0;

    you will not get this message:
    “An automated WordPress update has failed to complete – please attempt the update again now.”

  14. Edwin Valdez says:

    Hello Matt! Thank you for your posts about maintenance mode. I´ve used it in my wordpress customers when I´m doing some changes and it works fine. But, in the last version of WordPress, I´ve not found the maintenance.php file. I need create it or wordpress must have this files to modify? Coould you help me please?

    Thank´s in advance.

    • Matt says:

      @Edwin Valdez: the maintenance.php file does not exist by default. It is an optional file that can be created and dropped into the wp-content directory.

  15. Ralph says:

    This works brilliantly. No need for a plugin, just make 2 simple files and done.
    thanks

Leave a Reply

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

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>