Enabling Custom Maintenance Mode Without a Plugin

October 25, 2015
Posted in: Code Snippets, How To, WordPress

When updating WordPress or its plugins, users who visit your site will get the default WordPress maintenance notification:

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

While this message is very to-the-point, it doesn’t look like your site, and doesn’t use similar language.

To spice up the maintenance message, create a file named maintenance.php in your /wp-content/ directory, and give it a nice layout. Keep in mind that we shouldn’t be using any theme-specific files, and should avoid any WordPress functions and plugins. It’s best to contain all assets in the maintenance file itself. If you run a multilingual site, you’ll need to add a message for each language.

Once your custom file is built and tested, we can force-activate maintenance to see it in action. To enable maintenance mode indefinitely, simply create a file in the root of your site named .maintenance. Edit the file to include the following:


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

Once you’ve uploaded the file, WordPress will see it and put itself in maintenance mode. Unfortunately, enabling maintenance mode in this way blocks access to the entire site – even /wp-admin/.

To allow logins and full use of /wp-admin/, update the .maintenance file with this code:


<?php
function is_user_logged_in() {
	$loggedin = false;
	foreach ( (array) $_COOKIE as $cookie => $value ) {
		if ( stristr($cookie, 'wordpress_logged_in_') )
			$loggedin = true;
	}
	return $loggedin;
}
if ( ! stristr($_SERVER['REQUEST_URI'], '/wp-admin') && 
	 ! stristr($_SERVER['REQUEST_URI'], '/wp-login.php') && 
	 ! is_user_logged_in() )
	$upgrading = time();
?>

This will identify any users who are already logged in, and any users who are trying to access the login page.

This method works well when replacing portions of your theme or doing some testing, but since WordPress automatically creates and deletes the .maintenance file during core and plugin updates, you’ll need to run the updates, then re-enable maintenance mode once complete.

Scott Buckingham

President / Owner
613-801-1350 x101
[email protected]
Scott is a WordPress expert who has worked on hundreds of web design and development projects. He excels at finding creative ways to solve technical problems. View full profile