$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Redirect Visitors to a Temporary Maintenance Page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Redirect Visitors to a Temporary Maintenance Page

matteradmin10PV0评论

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.

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.

Share Improve this question edited Jun 17, 2015 at 2:50 shea 5,6724 gold badges39 silver badges62 bronze badges asked Nov 26, 2014 at 13:37 NewUserNewUser 5572 gold badges9 silver badges19 bronze badges 3
  • copy the code from a plugin? – Mark Kaplun Commented Nov 26, 2014 at 13:56
  • Why reinvent the wheel? Use a plugin. Specifically, Restricted Site Access. – vancoder Commented Nov 26, 2014 at 17:54
  • 2 I try to use as less plugins as possible for my custom theme. "Less is more" ;) – NewUser Commented Nov 26, 2014 at 18:11
Add a comment  | 

2 Answers 2

Reset to default 28

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.

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.

Post a comment

comment list (0)

  1. No comments so far