a da dads
 

How To Set Up A WordPress “Under Construction” Environment

Update: A new version of this script can be found here

It’s happened to all of us. We’ve been working on a site for weeks. Everything works correctly on our local server and on our development server. So launch day comes and we copy all of our files to our production folder…and then…!Errors Everywhere!. Php and Apache configuration settings are not the same. Urls are ending up as 404 Errors and images are loading from the incorrect path. Not to worry! In this tutorial I’ll show you the technique I came up with to have WordPress installed on your production server and show everyone a “Under Construction/Under Maintenance” page while you and only you can see the site as if it were live.

Update: A new and improved version of this solution has been posted here

Rename index.php

Open up the root folder of your WordPress installation and locate the index.php file. Rename this file to index_wp.php. Don’t worry, we’ll be loading this file later.

Create a new index.php file

Fire up your favorite code editor and create a new file called index.php and save it on the root folder of your WordPress installation.

Paste this code in the new index.php file

Copy and paste this code into your new index.php file. Remember to save it again.

<?php
//this is the first line of code that gets executed. We start the php session engine
session_start();

//if GET vars ‘uc’ and ‘pwd’ are correct, set session var ‘under_construction’ to true. Change the hardcoded password for any of your choice
if (isset($_GET['uc']) && $_GET['pwd'] == '123456'){
    $_SESSION['bypass_under_construction'] = true;
}elseif(isset($_GET['stop'])){
    $_SESSION['bypass_under_construction'] = null;
}

//check if current session is bypassing under construction page
if (isset($_SESSION['bypass_under_construction'])){
    //pull index_wp.php. This allows normal execution of your WordPress Installation
    include('index_wp.php');
}else{
//show or include your under construction page
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
    <head>
        <title>My Under Construction Page</title>
    </head>

    <body>
        <h1>This site is under construction or under maintenance</h1>
    </body>
</html>

<?php
//closing else curly bracket
}
?>

Open your site

If you have correctly performed the previous steps you should have a WordPress installation with a custom index.php file and a index_wp.php file, both on your root installation folder. Open up your home URL and you should see your “Under Construction” page. Whoo Hoo! now everyone is seeing this page. But now we need to continue development or perhaps show our clients the current progress on their site. Head on to your browser’s address bar and type this after your home URL.

/?uc=true&pwd=123456

Your address bar should look something like

http://www.myawesomewordpresssite.com/?uc=true&pwd=123456

Hit enter and there you have it! A fully functioning WordPress installation visible only to you during the complete length of your session. No redirects, no rerouting, no separate development and production folders. The code starts the session engine and looks for two variables sent as GET. The first variable is ‘uc’ (short for Under Construction). If this variable is set with any value then the var ‘pwd’ is checked to see if it matches the hard coded password. If these conditions are met a session variable is set to include the index_wp.php during the complete length of that session. If the conditions don’t match then the current session is forwarded to the “Under Construction/Under Maintenace” page and the WordPress installation never gets loaded.

Set your own password

Now for increased safety open the new index.php file and search for this line

if (isset($_GET['uc']) && $_GET['pwd'] == '123456'){

In this line change the ‘123456’ to the password of your choice. To end the development session type

/?stop=true

And the development session will be closed and you’ll be back on the “Under Construction” page. To deploy your website simply rename the new index.php file to index_uc.php and then rename the index_wp.php file to index.php. If you ever need to set your website in a “Under Construction/Under Maintenace” state, simply rename the files and your done!

Hope you find it useful, this piece of code has saved me hours of file transfers, reconfiguration and debugging time. If you like it or have some suggestions on how to improve this tutorial let me know in the comments!

 

Posted on PHP/ Tutorials/ Wordpress

By: juancamilo.estela

Tags: ,

 

22 Comments to “How to set up a WordPress “Under Construction” environment”

  • IceZ3ro says...

    Thanks Juan! I have been looking for a solution like this for a long time. I am a designer not a programmer but the simplicity of your solution is working like a charm on my current project.

    Reply - Proudly said on April 28, 2010 at 4:43 pm
  • Stone Deft says...

    Gee tnx … This is much better than what I have in mind (using ip address)

    You look great !

    Reply - Digitally recorded on May 3, 2010 at 1:12 am
  • Russell says...

    I had to change line 9 to:
    unset($_SESSION[‘bypass_under_construction’]);

    Otherwise the isset() test on line 13 always evaluates to true.

    Reply - Screamed on May 31, 2010 at 11:09 pm
    • That’s right Russel, you should use unset($_SESSION[‘bypass_under_construction’]); or $_SESSION[‘bypass_under_construction’] = null;
      Note: The code in this post has been updated with Russel’s feedback. Thanks Russel

      Reply - Whispered on June 1, 2010 at 1:12 am
  • Buy Zenerx says...

    Hey, nice site you got here! Keep up the excellent work!

    Reply - Spoken wisely on June 11, 2010 at 1:38 pm
  • Amy says...

    This is great! But when I click on any link from the
    “http://www.myawesomewordpresssite.com/?uc=true&pwd=123456” page
    I get the “Under Construction” page. Am I missing something?

    Reply - Digitally recorded on June 12, 2010 at 12:16 am
    • juancamilo.estela says...

      Hi Amy, sorry for such a late reply. I’ve been busy with all the name changing and legal stuff of my site. Have you tried checking if your php has sessions activated? If you’d like leave me a message and we can dig depper on the issue and get the code working.

      Reply - Spoken wisely on July 2, 2010 at 10:09 pm
  • Barney says...

    Thanks for this post. I’ve been looking for this solution for a while. One question before I do this…how do I remove the under-construction site?

    Reply - Whispered on June 16, 2010 at 6:54 pm
  • Barney says...

    Figured it out…thanks

    Reply - Digitally recorded on June 16, 2010 at 9:45 pm
  • Zach says...

    Great help man so awesome just what I was needing

    Reply - Screamed on September 1, 2010 at 2:58 am
  • Mark says...

    Hi Juan, I really like your under-construction workaround, but I have the problem that Amy was having, where any link selected from the newly minted index_wp.php page takes me to the under construction page instead, rather than the intended page. Did you two ever come up with a solution? No php sessions activated here. And I tried changing the permalinks format in WordPress, but that just gave me 404 errors for every link (including “Home”) that I tried to select.

    Reply - Whispered on January 5, 2011 at 6:12 pm
  • Mark says...

    Well, it’s like pulling teeth at 1and1 to get them to give me a decent answer re: php sessions. They were supposed to send me info I need to put in a php.ini file, but haven’t done so yet. I DID check my index.php file and it has the “sessions_start” command right at the top. Isn’t that what you mean? I tried Googling the “activate sessions” to see what was out there, but didn’t find much that was useful. As much as I like this “under construction” process you’ve developed, I’m going to have to discard it if I can’t move beyond the home page and see the other page output during the development process.

    Reply - Screamed on January 7, 2011 at 4:57 pm
    • Maha says...

      I agree with you Mark.. Same problem here.. let me know if u found a solution.

      Reply - Digitally recorded on June 29, 2011 at 2:36 am
  • molchkoff says...

    Many thanks for publishing this information. It is really helpful for me.

    Reply - Proudly said on February 4, 2011 at 1:51 pm
  • shah says...

    if u want to see ur site, just put http://sample.com (without www)..

    Reply - Digitally recorded on June 17, 2011 at 7:59 am
  • Maha says...

    I LOVE IT! thank you 😀

    Reply - Digitally recorded on June 18, 2011 at 6:18 am
  • Kyle says...

    This is great! Accept when A site has already been viewed and searched by google… the existing google links still point to active pages.

    How can I avoid ALL pages from being viewed? not just the direct URL?

    Is is something I can do in my php5.ini file or .htaccess or…?

    Please help!

    Reply - Spoken wisely on July 13, 2011 at 5:39 am
  • Ok, I see. This works great. Brilliant method. So surprised there is not a plugin for this. I have looked all over for this method.

    Thanks

    Reply - Proudly said on October 9, 2011 at 8:05 pm
  • Andic says...

    You rock… thanks so much for this.

    Reply - Whispered on December 7, 2011 at 1:07 am
  • maxtraffic says...

    Thanks mate, This is what I was looking.. Great help.

    Reply - Digitally recorded on October 16, 2012 at 4:16 pm

Leave your Reply to Stone Deft

Click here to cancel reply.