Easily configure multiple domains/hosts for a single WordPress setup using arrays rather than constants
<?php
/**
* Easy WordPress configuration
*
* @param array $settings An array of named settings
* @param bool $debug An optional boolean to output errors to the screen
*/
function wp_configure($settings, $debug = false)
{
$host = $_SERVER['HTTP_HOST'];
$values = isset($settings['defaults']) ? $settings['defaults'] : array();
$params = isset($settings[$host]) ? $settings[$host] : null;
if(is_string($params)){ $params = $settings[$params]; }
if( ! $params ) { die('<body style="font-family:sans-serif"><h1>Configuration error</h1><p>The array passed to wp_configure() is missing an entry for "' .$host. '".</p>'); };
foreach($params as $key => $value) { if( ! empty($value) ) { $values[$key] = $value; } }
foreach($values as $key => $value) { define($key, $value); }
$debug && print ('<pre>' . print_r($values, 1));
}
/**
* Configure WordPress based on the HTTP_HOST
*/
wp_configure(array
(
// default values
'defaults' => array
(
'DB_HOST' => 'localhost',
'DB_NAME' => 'blog',
'DB_USER' => 'root',
'DB_PASSWORD' => '',
'WP_HOME' => '',
'WP_SITEURL' => '',
'DB_CHARSET' => 'utf8',
'DB_COLLATE' => '',
),
// the local domain
'domain.local' => array
(
'DB_HOST' => '',
'DB_NAME' => '',
'DB_USER' => '',
'DB_PASSWORD' => '',
'WP_HOME' => '',
'WP_SITEURL' => '',
),
// the live domain
'domain.com' => array
(
'DB_HOST' => '',
'DB_NAME' => '',
'DB_USER' => '',
'DB_PASSWORD' => '',
'WP_HOME' => '',
'WP_SITEURL' => '',
),
// alias one domain to another
'www.domain.com' => 'domain.com',
// add any other domains here
));