setting up a wp dev environment on a live site

developing a wordpress site on the domain it eventually will live on has plenty of advantages (and ok, a few obvious disadvantages not covered in this post…). no need to fiddle around in the db looking for hardcoded urls to the development domain name and when it’s time to launch – you don’t need to bother with db export/imports and stuff.

but – how to do this when there’s already an existing site live on this domain? actually it’s accomplished pretty easily.

make a folder on your live site, call it something like “dev”, put this into the index.php file within this folder:

if(isset($_GET["mode"])&&$_GET["mode"]=="dev"){
	setcookie("dev","true", time()+3600*24*7, "/");
	header("Location: /dev");
}else if(isset($_GET["mode"])&&$_GET["mode"]=="live"){
	setcookie("dev", "", time()-1000, "/");
	header("Location: /dev");
}
if(isset($_COOKIE["dev"])){
	echo "DEVELOPMENT MODE IN YOUR BROWSER: ON | SWITCH";
}else{
	echo "DEVELOPMENT MODE IN YOUR BROWSER: OFF | SWITCH";
}

when a user enters /dev on your live site and clicks ON, a cookie will be set. this cookie can then be checked for, and the server can make a decision wether to show the old site or the development site.

next upload wordpress the usual way to the docroot of the site, and install. make sure you don’t overwrite any existing files on the server when uploading.

lastly – let’s make that cookie work for us. open the file wp-blog-header.php, located in the docroot, and replace the contents with this:

if(isset($_COOKIE["dev"])){ # does the user have the dev cookie?
	if ( !isset($wp_did_header) ) {
		$wp_did_header = true;
		require_once( dirname(__FILE__) . '/wp-load.php' );
		wp();
		require_once( ABSPATH . WPINC . '/template-loader.php' );
	}
}else{
	header("Location: currentindex.htm"); # no? then show the current page
}

one word of warning though – do not run wordpress auto update while developing the site this way. doing so will remove the cookie-check in wp-blog-header.php and exposing your development site to the world. should you need to run the automatic update, remember to make the changes to wp-blog-header.php again.

when you’re ready to go live with your shiny new wordpress site – just remove the cookie check from wp-blog-header.php and let the world in. that’s it folks!