WordPress’s maintenance mode — how to enter and leave it

Writing a WordPress plugin I had to dig around to figure out maintenance mode. That’s when visitors get this message.

Briefly unavailable for scheduled maintenance. Check back in a minute.

It’s easy to do from plugin or theme code.

To enter maintenance mode

Write a file named .maintenance into your document root with something like these contents

<?php
$upgrading = time();
?>

To leave maintenance mode

Delete the .maintenance file.

Details

As you probably guessed, .maintenance contains a very short php program. It sets the $upgrading variable to the time when maintenance started. The way WordPress works, if you don’t delete the file, it assumes that maintenance ends ten minutes (600 seconds) after the time in that variable.

Programming

To enter maintenance mode do something like this:

$maintenanceFileName = ABSPATH . '.maintenance';
$maintain     = array();
array_push( $maintain,
	'<?php',
	'$upgrading = ' . time() . ';',
	'?>' );
file_put_contents( $maintenanceFileName, implode( PHP_EOL, $maintain ) );

To leave it do something like this:

$maintenanceFileName = ABSPATH . '.maintenance';
unlink( $maintenanceFileName );

Leave a Comment