Updating your website can take some time, and while you do it, a maintenance notice is always better than a non-functioning website. It’s especially important to use a proper maintenance notice page. On big websites which are regularly crawled by search engines, it is very important not to 404 your pages for search robots.
In WordPress, the default maintenance message is invoked automatically when upgrading your site. While it gives the proper response headers (503 – Service Unavailable), it is quite an ugly message. I recently created a (switchable) WordPress custom maintenance mode (and custom maintenance page) for a WordPress website I have been developing. All without the tedious process of uploading a .maintenance file, or installing a plug-in. It is quite simple to do, I’ll explain below:
WordPress custom maintenance page
You can make your own custom maintenance page by creating a maintenance.php
file, and put it directly inside (in the root of) the wp-content
folder. You can do whatever you want in this file; just make sure it can function on its own and doesn’t rely on your theme (depending on the nature of your maintenance work, your theme might not be available). There is just one important bit to add: proper response headers. You can do this by adding the following PHP-code at the top of the file:
<?php
$protocol = "HTTP/1.0";
if ( "HTTP/1.1" == $_SERVER["SERVER_PROTOCOL"] )
$protocol = "HTTP/1.1";
header( "$protocol 503 Service Unavailable", true, 503 );
header( "Retry-After: 3600" );
?>
You can specify the Retry-After
bit if you like (3600 seconds = 60 minutes). For more info, check out Yoast’s article on maintenance mode.
When you are done upload the file. WordPress will then use this file automatically (if it is in the wp-content
root) when it engages maintenance mode.
WordPress manual maintenance mode
To trigger maintenance mode, without a plug-in or adding a manual .maintenance
file, add the following code to your theme’s functions.php
file, preferrably at the beginning.
define('MAINTENANCE', false); //set to true to enable maintenance mode
if (!is_admin() && !current_user_can( 'manage_options' ) && MAINTENANCE) { //envoke maintenance if set
if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
require_once( WP_CONTENT_DIR . '/maintenance.php' );
die();
}
}
This will activate maintenance mode if the MAINTENANCE
constant is set to true (first line), but it will not activate maintenance for the admin area, nor for users that have admin-privileges. This differs from WordPress’ own maintenance mode (not even the admin panel will be available during WordPress maintenance mode), and allows you to test things while your regular visitors will be presented with the maintenance page you created earlier.
Notice
Important notice: as you specify the maintenance mode in your current theme’s functions.php
, be aware that if you switch themes during maintenance, you make sure that the theme you switch to also has these lines, and has maintenance mode activated! Otherwise, maintenance mode will be deactivated on switching themes, potentially showing your visitors an untested theme.
Hope this is of use to someone!
Actually, scratch my comment. I was checking via a browser I’d logged out of
So, it works 😉
Thanks for this post. But when maintenance mode is set to “true”, I’m not able to access the wp-admin area. I used your code and a simple line of HTML. My message appears, but the function doesn’t appear to work as you describe.