Flexible wp-config on localhost: many installs, maintained by cookies

This allows you to run multiple, ver different sites off one wordpress install by sharing one wp-config but having it direct requests to different databases based on the domain / location you’re accessing from.

So maybe you’ll have demo.localhost.com and demo2.localhost.com and you want them to share the same resources but go to different databases and you don’t want to keep switching the include in wp-config. This will let you pass a variable to the URL and then store it in a cookie that’s then saved each time you visit that domain. We couple that with giving each domain a unique media file folder location so files are commingled and that’s it! Works like a charm.

There are likely other ways to handle but this this worked for me and allows me to keep on using the basic MAMP settings on my Mac.

Steps:

  1. Pass the name of the wp-config file you want to use via URL on the domain you want to use that wp-config on.
  2. Reload the page and make sure all is okay.
  3. You can now revisit this page as normal and the value will be stored in the cookie.

CoDE:

<?

$currentserver = $_SERVER['SERVER_NAME'];
$currentserver_cleaned = str_replace(array('.', ','), '' , $currentserver);
$cookie_name = 'db_'.$currentserver_cleaned;
if (isset($_GET['db'])) { 
	$currentsite =  $_GET['db']; 
	$cookie_value = $currentsite;
	setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
} elseif(isset($_COOKIE[$cookie_name])) {
   	//echo "Cookie '" . $cookie_name . "' is set!<br>";
    //echo "Value is: " . $_COOKIE[$cookie_name];
    $currentsite = $_COOKIE[$cookie_name];
} else {
	//echo 'sss';
	$currentsite = 'cherry';
}




//define( 'WP_DEBUG', true );
// define('WP_HOME','http://demo.localhost.com');
// define('WP_SITEURL','http://demo.localhost.com');
// LOCAL!!!

if ($currentsite == 'surgo') { 

	define( 'UPLOADS', 'wp-content/uploads-localhost' );
	include('wp-config-includes/local-multi-surgoapp.php');

} elseif ($currentsite == 'cherry') {

	define( 'UPLOADS', 'wp-content/uploads-localhost' );
	include('wp-config-includes/local-multi-test.php');

} else {
	echo 'You have not selected a site database!';
}

?>