After using many wordpress maintenance mode plugins I decided that they all had a problem in that they relied on the database to either redirect or display a maintenance page. If the site is going into maintenance mode so you can work on the database then this could present a problem.
The following maintenance mode code is easy to turn on and off via a URL and simple to implement, it also utilises a cookie bypass to allow you to continue working on the site whilst everyone else is presented with a simple maintenance page and reports a 503 Service Unavailable to search engines.
Once complete you will have the following maintenance mode options via a URL:
To enter into maintenance mode and set bypass cookie which allows access to the site whilst in maintenance:
domain.com/maintenance-switch.php?key=MY-SECRET-KEY&mode=enable
To exit from maintenance mode:
domain.com/maintenance-switch.php?key=MY-SECRET-KEY&mode=disable
To just set the bypass cookie which allows access to the site whilst in maintenance:
domain.com/maintenance-switch.php?key=MY-SECRET-KEY
Step 1: Create the maintenance page.
Copy the code below and edit it to your needs, at the very least you should change the domain.com
and phone number elements.
Save the code as maintenance.php and upload it to your website root so that it can be accessed via domain.com/maintenance.php
The maintenance.php has a redirect so that after 30 second the visitor is redirected to the homepage unless it’s still in maintenance mode.
Tip:
You can also copy maintenance.php and rename it to db-error.php then upload to domain.com/wp-content
, this will be automatically displayed instead of the not very helpful Error Establishing a Database Connection text in the event of this error occurring.
Step 2. Edit the htaccess file.
Make a backup then edit domain.com/.htaccess file.
The following code block needs to be added to the very beginning of the file. You will need to change the text UNIQUE-COOKIE-NAME
to something else of your choosing.
Step 3. Add the maintenance-switch file.
Copy the code below and change MY-SECRET-KEY
. This is the key that will be included in the URL to enable and disable maintenance mode, also edit the UNIQUE-COOKIE-NAME
to match as per above.
Save the code as maintenance-switch.php and upload it to your website root so that it can be accessed via domain.com/maintenance-switch.php
How it works
When you visit the URL domain.com/maintenance-switch.php?key=MY-SECRET-KEY&mode=enable
to enable maintenance mode.
MY-SECRET-KEY is checked to ensure you have access to run the code.
The bypass cookie is set (maintenance-mode-bypass-UNIQUE-COOKIE-NAME).
A file named maintenance.enabled is created on the hosting root (this file is the indicator to the htaccess file)
Any visitors to your site will now see the maintenance page. This is handled by the htaccess file that checks for the existence of the maintenance.enabled file in the root, if found and the cookie is not set then redirect to maintenance.php.
When you visit the URL domain.com/maintenance-switch.php?key=MY-SECRET-KEY&mode=disable
to disable maintenance mode.
The previously created file named maintenance.enabled is deleted
Any visitors to your site will now see the site as normal. So basically if the file maintenance.enabled is the switch for enabling and disabling the maintenance mode and the cookie just allows you to bypass the rules set in the htaccess.
domain.com/maintenance-switch.php?key=MY-SECRET-KEY
can be used to set the cookie for other developers to work on the site.
Leave a Reply
Want to join the discussion?Feel free to contribute!