WordPress Multi-Environment

October 31, 2019
By

What is it?

This tool will help you easily move your WordPress site from environment to environment without having to write database queries or change the wp-config.php file every time.

Where can I get the code?

You can find the code on my GitHub repository: WordPress-Multi-Environment.

How do I use it?

  1. Upload the directory ‘site-settings’ to the root of your WordPress site.
  2. Open up your wp-config.php file and add the following line:
require_once(__DIR__ . '/site-settings/site-settings.php');
  1. If you visit your site, a new site-settings.config.php file will be created for you in the directory. Go ahead and move your WordPress credentials into this document.
  2. In my repo you will see a sample wp-config.php file. Replace your credentials in your wp-config with the code from my sample.
/** The name of the database for WordPress */
define( 'DB_NAME', WPSiteSettings::getSetting('dbname') );

/** MySQL database username */
define( 'DB_USER', WPSiteSettings::getSetting('user') );

/** MySQL database password */
define( 'DB_PASSWORD', WPSiteSettings::getSetting('pass') );
/** MySQL hostname */
define( 'DB_HOST', WPSiteSettings::getSetting('host') );
  1. Inside the site-settings.config.php you will see a “servers” array. Inside here you can create an array for each environment you plan to use.

Samples

<?php

function getConfig_WPSiteSettings() {
	return array(
		'multisite' => false,

		'servers' => array(
			array(
				// DB Arguments
				'host'   => '',
				'user'   => '',
				'pass'   => '',
				'dbname' => '',

				// Multisite Arguments
				'subdomains' => false,

				// Site(s) on this server
				'sites' => array(
					// DOMAIN => BLOG_ID
					'https://www.domain.com' => 1 // no trailing slash
				)
			),

			// localhost
			array(
				// DB Arguments
				'host'   => '127.0.0.1',
				'user'   => 'root',
				'pass'   => '',
				'dbname' => 'dbname',

				// Multisite Arguments
				'subdomains' => false,

				// Site(s) on this server
				'sites' => array(
					// DOMAIN => BLOG_ID
					'http://localhost/devsite' => 1
				)
			)
		)
	);
}
<?php

function getConfig_WPSiteSettings() {
	return array(
		'multisite' => true,

		'servers' => array(
			array(
				// DB Arguments
				'host'   => '',
				'user'   => '',
				'pass'   => '',
				'dbname' => '',

				// Multisite Arguments
				'subdomains' => false,

				// Site(s) on this server
				'sites' => array(
					// DOMAIN => BLOG_ID
					'https://www.domain.com' => 1, // no trailing slash
                    'https://www.anotherdomain.com' => 2,
				)
			),

			// localhost
			array(
				// DB Arguments
				'host'   => '127.0.0.1',
				'user'   => 'root',
				'pass'   => '',
				'dbname' => 'dbname',

				// Multisite Arguments
				'subdomains' => false,

				// Site(s) on this server
				'sites' => array(
					// DOMAIN => BLOG_ID
					'http://localhost/devsite' => 1,
                    'https://localhost/anothersite' => 2,
				)
			)
		)
	);
}