11

To prevent my visitors from seeing a broken version of my site during maintenance, and to give them a heads up on the updates, I would like to redirect them automatically to a temporary maintenance page. I am looking for a portable solution which can be used on any site, without hardcoding URLs.

Logged in administrators (or other user level of choice) should get full access to the back-end and the front-end. There are a lot of plugins out there that offer this functionality, but I'm looking for a code-only solution.

  • copy the code from a plugin? – Mark Kaplun Nov 26 '14 at 13:56
  • Why reinvent the wheel? Use a plugin. Specifically, Restricted Site Access. – vancoder Nov 26 '14 at 17:54
  • 2
    I try to use as less plugins as possible for my custom theme. "Less is more" ;) – NewUser Nov 26 '14 at 18:11
22

WordPress has an embedded feature for handling maintenance mode.

When you upgrade a plugin, or WordPress core from WP dashboard, WordPress enters maintenance mode: it tries to load a file named maintenance.php located in the content folder (usually /wp-content), and if that file is not there, WP shows a default message.

I suggest you use that file, in this way you'll be consistent for your manually-triggered maintenance and for WordPress-handled maintenance.

How To

  1. First of all create the maintenance.php file and put there the content you want. For styling I suggest you put CSS in the file itself, using <style> tag; generally this is not good advice, but in this case it gives you the ability of using the file for WordPress-handled maintenance mode, when no theme is loaded (and the theme may be upgrading, so not reliable).

  2. Save the file just created in content folder (usually /wp-content).

  3. In your functions.php put:

    add_action( 'wp_loaded', function() {
        global $pagenow;
        if(
            defined( 'IN_MAINTENANCE' )
            && IN_MAINTENANCE
            && $pagenow !== 'wp-login.php'
            && ! is_user_logged_in()
        ) {
            header( 'HTTP/1.1 Service Unavailable', true, 503 );
            header( 'Content-Type: text/html; charset=utf-8' );
            header( 'Retry-After: 3600' );
            if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
                require_once( WP_CONTENT_DIR . '/maintenance.php' );
            }
            die();
        }
    });
    

    This code will check a constant (see next point) and if user is not logged in, load the file created at point #1 and exit.

    If you want to allow only users with specific capabilities, use current_user_can('capability_to_allow') instead of is_user_logged_in(). See Codex for more info.

    Maybe you can add to maintenance.php a link to login page; in this way a non-logged user can click it without having to manually enter the login URL in the address bar.

    If you are using a theme developed by a 3rd party, use a child theme; in this way you will be able to safely update the theme without losing your changes.

  4. When you want to enable maintenance mode, open your wp_config.php and put there:

    define('IN_MAINTENANCE', true);
    

    After that, when you are ready to make your site public again, just remove that line or change true to false for easier re-enabling.

  • This is a very detailed answer and a great way to solve my problem with less effort as possible! I will try it later and accept the Answer if it works. Thank you so much G.M! – NewUser Nov 26 '14 at 18:16
  • Hi G.M - i use a custom named "wp-content" directory but i have no idea how to change the WP_CONTENT_DIR..... to make it work? best – NewUser Nov 30 '14 at 16:44
  • Old thread, I know.. but just in case someone need that answer, It's as easy as adding define('WP_CONTENT_DIR','/url/to/define/'); in your wp-config.php file. – gmo May 16 '15 at 15:17
  • @gmazzap, this is amazing. Does it mean that if and only if you are already logged (say as an administrator), you will still be able to access and make modifications to/on the admin console? – paranza Mar 11 '17 at 20:01
  • @paranza yes, correct. – gmazzap Mar 12 '17 at 10:08
2

The previous answer is complete and well written. Anyhow if you are like me and you want to have everything in one place you can drop the following lines in the function.php file and create a maintenance.php file in your theme directory.

This is especially useful if your Git repository points just to the theme directory.

add_action( 'wp_loaded', function() 
{
    global $pagenow;

    // - - - - - - - - - - - - - - - - - - - - - - 
    // Turn on/off you Maintenance Mode (true/false)
    define('IN_MAINTENANCE', true);
    // - - - - - - - - - - - - - - - - - - - - - - 

    if(
        defined( 'IN_MAINTENANCE' )
        && IN_MAINTENANCE
        && $pagenow !== 'wp-login.php'
        && ! is_user_logged_in()
    ) {
        header('HTTP/1.1 503 Service Temporarily Unavailable');
        header( 'Content-Type: text/html; charset=utf-8' );
        if ( file_exists( get_template_directory() . '/maintenance.php' ) ) {
            require_once( get_template_directory() . '/maintenance.php' );
        }
        die();
    }
});

NOTES

I changed the header to header('HTTP/1.1 503 Service Temporarily Unavailable'); as the one above didn't work for me.

  • I can see the point of this, but note a couple of things - WordPress won't use this maintenance file within the theme dir when doing its own maintenance (so it would be a good idea to create wp-content/maintenance.php as well and then require() your theme one into it); also defining constants like that would usually sit in your wp-config.php or as an environment variable, rather than having to manage maintenance mode being on/off by making commits. :) – Tim Malone Dec 8 '17 at 0:37
  • @TimMalone, I do agree. At the end it's a workaround which will work well on a short time period, while you (developer) make all the updates. – a.barbieri Dec 8 '17 at 10:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.