Redirect Visitors to a Temporary Maintenance Page

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.


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

    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.

Leave a Comment