Tuesday, December 20, 2011

How To Put Your WordPress Website in Maintenance Mode Without a Plugin


                                                                               
In this tutorial, I will show you how to easily redirect your visitors to a temporary maintenance page without the need of a plugin. The custom maintenance page lets your visitors know that your WordPress Website (or blog) is down for maintenance. Logged-in administrators will still have full access to the Website while visitors see a customized maintenance message.

From time to time you may have to bring down your WordPress Website for maintenance. For instance, If you want to redesign, add new features, change the layout or other significant changes to your Website, then you don´t want your visitors to see your “in progress” changes or broken Web pages. In order to prevent your users from seeing a broken version of your Website, it is crucial that you redirect visitors to a temporary maintenance page.

What about a plugin?

Yes, there are plugins that redirect visitors to a custom maintenance page. But I encourage you to read my previous post about unnecessarily adding plugins to your theme.

There are 2 scenarios when a Website is put into maintenance mode:

  1. Automatic: Executed during an automatic WordPress version update from your dashboard.
  2. Manual: Executed by you at any time you see fit.

1. Automatic Maintenance Mode (Done by WordPress)

When you are performing an automatic WordPress version update from your dashboard, WordPress temporarily puts the Website in maintenance mode until the upgrade is complete.

2. Manual Maintenance Mode (Done at Anytime by You)

The second scenario is when, at a time of your choosing, want to put your Website in maintenance mode. let´s say you are adding custom functionality to your Website and you need an hour to finish the task. This is where you can put your Website into maintenance mode.

Final Result | Manual Maintenance Mode

The picture below shows the temporary maintenance page that your visitors will see, while at the same time logged-in administrators still have full access to the Website.
Example of the WordPress maintenance page.

Features of The “Manual Maintenance Mode” Code

What does my Code do? It adds a maintenance page to your WordPress Website that lets visitors know your Website is down for maintenance. Logged-in administrators (and Super Admins) can continue to work on the Website behind the scene and view those edits online. At the same time, all your visitors and other users only see the maintenance page.
Here are few features of the code:
  1. The code has to be manually activated and deactivated.
  2. Visitors will see a customized, user-friendly maintenance message.
  3. Maintenance message is customizable. You can edit the message as you see fit. For instance, you can specify the time that your Website will be down for maintenance.
  4. Logged-in administrators and Super Admins, get full access to the Website (dashboard/back-end and front-end).
  5. The title of the maintenance page (which also appears on the browser´s tab) is customizable.
  6. Code is tested to work properly on WordPress Version 2.8 and newer.
  7. Code is tested to work properly with all major browsers.

“Manual Maintenance Mode” Code | functions.php

When you are ready to put your Website in Maintenance mode, open the functions.php file located in your theme´s folder, and add (copy and paste) the following code (CODE-1). Your WordPress Website is now NOT available to anyone except administrators.
CODE-1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/***************************************************************************
* @Author: Boutros AbiChedid
* @Date:   November 14, 2011
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/
* @Description: Function that puts WordPress Website in maintenance mode.
* @Tested on: WordPress version 3.2.1 (but it works on earlier versions.)
****************************************************************************/
function activate_maintenance_mode() {
    //If the current user is NOT an 'Administrator' or NOT 'Super Admin' then display Maintenance Page.
    if ( !(current_user_can( 'administrator' ) ||  current_user_can( 'super admin' ))) {
        //Kill WordPress execution and display HTML maintenance message.
        wp_die('<h1>Website Under Maintenance</h1><p>Hi, our Website is currently undergoing scheduled maintenance.
        Please check back very soon.<br /><strong>Sorry for the inconvenience!</strong></p>', 'Maintenance Mode');
    }
}
//Hooks the 'activate_maintenance_mode' function on to the 'get_header' action.
add_action('get_header', 'activate_maintenance_mode');
?>

CODE-1 Notes:

When you are finished with your Website´s maintenance, make sure to disable maintenance mode by commenting out line 18 of the code. Test to make sure that your Website is back online. No need to remove the code from your functions.php file.
WordPress will not know your role unless you are logged-in to your WordPress dashboard.
If you render your Website in a different browser than where Administrators and Super Admins are logged-in, then the Website will be in maintenance mode. WordPress does not know that you are logged-in in a different browser.
The second parameter of the wp_die() function define the $title variable. This is the HTML title tag of the maintenance page which also appears on the browser´s tab. This is customizable and is better than the default $title variable (´WordPress > Error´). For instance, you can change the $title variable to Your blogname – Maintenance Mode.
wp_die() function accepts some HTML formatting.
CODE-1 works for WordPress 2.8 and above. But I hope that you will upgrade to the latest WordPress version.

Code References:

General Warning

When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag (<?php) or after the closing PHP tag (?>). The correct way is like so:
1
2
3
4
5
6
7
<?php
//Some Code here beetween the opening PHP tag (above)
//and the closing PHP tag (below)...
?>
<?php
//Some other Code here ...
?>
In the above code, if you leave any white space or a newline between lines 4 and 5, you will get the following error: “Warning: Cannot modify header information - headers already sent by…

HELP! I got stuck in Maintenance Mode. What Should I do?

First don´t panic! It is counter-productive.
  1. If you manually put your wordPress Website in Maintenance mode, using my code (CODE-1), then make sure to go to your functions.php file located in your theme´s folder and comment out line 18 of CODE-1. This will disable the function.
  2. If you were doing an automatic WordPress version update from your WordPress dashboard, then that´s a different and unrelated story. In this case, make sure to manually delete the .maintenance file using your FTP software, then do a manual WordPress upgrade.
    Note that the .maintenance file is located in the root of your WordPress install (same level as wp-settings.php). See the references below for more details.

    References:

Your Turn to Talk

At some point, every Website has to perform maintenance of some sort that requires taking the Website offline for sometime. The best way to do that is to keep your visitors informed by redirecting them to a temporary customized maintenance page, and all this without the need of a plugin.
How easy did you find this tutorial to implement? Do you have something to add or anything else to say? If so, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.


For Further Reading,
Tutorial

0 comments:

Post a Comment