How to Put Your WordPress Site in Maintenance Mode without a Plugin

Sometimes you may want to put your site into maintenance without a plugin. The easiest way to do so is to use the built-in maintenance mode in WordPress.

WordPress own maintenance mode gets activated every time you update your WP core, plugins or themes from the WP Dashboard. But we can also activate it ourselves by placing a file named .maintenance in the root directory of our WordPress install. In the file it is also necessary to use a variable named $upgrading that stores a timestamp. When created by WordPress update process, $upgrading stores the timestamp the file was created, but we can use different values to modify the built-in behaviour.

Warning: If the difference between the current time and the value of $upgrading is bigger than 10 minutes, your site will exit maintenance mode and visitors will see the normal pages. In the admin dashboard however you will get a warning: “An automated WordPress update has failed to complete – please attempt the update again now.” Deleting .maintenance gets rid of the warning.

wp update fail warning

Let’s run through various settings for the maintenance time.

Infinite maintenance

If you want to keep your site in maintenance indefinitely just assign the current timestamp to $upgrading:

<?php
$upgrading = time();

For the next examples go to WP Admin > Settings > General and get the current time from the ‘Universal time (UTC) is ‘ line. Use it to replace ‘2018-03-13 17:34:42’ in the code examples below:

1 week maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+1 week' );
$upgrading = $dt->format( 'U' );

3 days maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+3 day' );
$upgrading = $dt->format( 'U' );

1 day maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+1 day' );
$upgrading = $dt->format( 'U' );

15 minutes maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+15 min' );
$upgrading = $dt->format( 'U' );

1 minute maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+1 minute' );
$upgrading = $dt->format( 'U' );

30 seconds maintenance

<?php
$dt = new DateTime( '2018-03-13 17:34:42' );
$dt->modify( '+30 sec' );
$upgrading = $dt->format( 'U' );

Customizing the maintenance message

wp unavailable

The default message is plain and simple: ‘Briefly unavailable for scheduled maintenance. Check back in a minute.’ It can be customized by placing a maintenance.php file inside the wp-content directory. If you are using a custom maintenance.php file do not forget to send the 503 status code.

If you are not planning on sending a Retry-After response you can use WordPress’s status_header function to send the status code.

<?php
require_once ABSPATH . WPINC . '/functions.php';
status_header( 503, '503 Service Temporarily Unavailable' );
?>
<h1>503 - Temporarily Closed For Maintenance</h1>
<p>We are currently updating our website. Sorry for the inconvenience.</p>
<?php exit(); ?>

If the Retry-After response must be sent (e.g. for 5 minutes maintenance), php’s header function must be used.

<?php
$protocol = wp_get_server_protocol();
header( $protocol . ' 503 Service Temporarily Unavailable', true , 503 );
header('Retry-After: 300'); // 300s = 5 minutes
?>
<h1>503 - Temporarily Closed For Maintenance</h1>
<p>We are currently updating our website. Sorry for the inconvenience. Check back in 5 minutes.</p>
<?php exit(); ?>

You may also like...

Leave a Reply

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