Setting up a WordPress Development Environment (and have it work)
One of the challenges developing in WordPress for me was setting it all up in a "localhost" environment. I went through a few trials with this but in the end, this is the best way that I have found. And it's easy.
If you have copied down a WP install to your development machine, fired up a web server and imported a backup of the database, you might be surprised to find that all of the links take you to the live site. You get it all set up then load http://localhost/, click any link and suddenly you are on the live site (and perhaps wondering "why the $*#%!???" none of the changes you've made are showing up!)
Here's your solution. You want to set a variable in your apache configuration that tells your install where you are.
# in the Apache config on the production machine: SetEnv client_environment "production"
You can't really use SetEnv in .htaccess if it's a part of your WP install because that file is copied up and down with your changes. It's part of your working directory for WP. In a pinch, you could probably put SetEnv into a .htaccess file but I only use the proper Apache configuration files, which are separate from my WordPress installs (but I also have my own server and have access to do so).
# in the Apache config on the localhost machine: SetEnv client_environment "development"
'client_environment' can be any word and "production" or "development" can be any value, but I use development, staging or production, depending on what server you are on. In this way, we can use apache's conf to tell our app what we are on. The apache conf isn't part of the repo (or, we have 3 copies of it).
// then in wp-config.php, toward the bottom:
// note: you can access any SetEnv variable through PHP's $_SERVER
switch ($_SERVER['client_environment'])
{
case 'development':
define('WP_SITEURL', "http://localhost");
define('WP_HOME', "http://localhost");
display_errors(E_ALL);
break;
case 'staging':
define('WP_SITEURL', "http://livewebsite.com");
define('WP_HOME', "http://stg.livewebsite.com");
display_errors(E_ALL);
break;
// in case we forget to SetEnv
// client_environment in apache conf file, default to production
default:
case 'production':
define('WP_SITEURL', "http://livewebsite.com");
define('WP_HOME', "http://livewebsite.com");
display_errors(0);
break;
}
If you are on a shared host and can't access the root-owned Apache configuration, you can ignore adding it to .htaccess and instead you can set it up on your localhost apache (you should have at least sudo rights on your local computer, right?!), and replace the switch statement like this:
// then in wp-config.php, toward the bottom:
if (isset($_SERVER['client_environment'])) // if isset() then you must be on localhost ...
{
define('WP_SITEURL', "http://localhost");
define('WP_HOME', "http://localhost");
display_errors(E_ALL);
} else {
define('WP_SITEURL', "http://livewebsite.com");
define('WP_HOME', "http://livewebsite.com");
display_errors(0);
}
Hope that helps!
