Running a Development Copy of WordPress

Running a Development Copy of WordPress into your local computer server

0 Shares
0
0
0
0
0
0
0

Whether you’re developing WordPress plugins, WordPress themes, or rolling out a new site based on a customized version of WordPress, it’s often helpful to be able to mirror your live site on your local system for development. This is common for commercial sites, where you have a live production server but you also need a development and/or staging server, which is run locally to test plugins, mods, themes, and everything else that you don’t want to do on the live production server.

For example, your live site could be http://www.example.com/blog and your development site might be http://localhost/example.com/blog.

However, WordPress serves pages with embedded absolute URLs based on the absolute site URL configured in your database, so none of the links on your development site will work.

Assuming that WordPress on your local machine is accessible via “http://localhost/local_folder_name“, then update the WP_OPTIONS for option_name siteurl and home to be “http://localhost/local_folder_name“.

SELECT * FROM wp_options WHERE option_name = "home" OR option_name = "siteurl";
UPDATE wp_options SET option_value = "http://localhost/local_folder_name" WHERE option_name = "home" OR option_name = "siteurl"

Each time you download a copy of your database from live server to your development server, you’ll have to remember to update your database again. Fortunately, there’s a simple hack for functions.php that can doesn’t require you to update your database. Note: with this hack activated, WordPress will IGNORE values you’ve configured for siteurl and home in your Options, and work them out for itself.

Run 2 Copies of WordPress from the same Database

WordPress 2.7 – 3.0

You need to add code to your wp-config.php that assigns the right values of WP_HOME and WP_SITEURL to reflect the current location of WordPress, whether it’s the live site or a local staging server.

To do this, simply add one of the following code sections to your wp-config.php above the last require_once statement.
Either this function:

function WP_LOCATION () {
    $script_path = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
    $wp_base_path = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
    $web_subfolder = substr( $script_path, strlen($wp_base_path));
    $wp_path = $web_subfolder ? substr( dirname($_SERVER['SCRIPT_NAME']), 0, -strlen($web_subfolder) ) : dirname($_SERVER['SCRIPT_NAME']) ;
    $retval = 'http' . ($_SERVER['HTTPS'] ? 's' : null) . '://' . $_SERVER['HTTP_HOST'] . $wp_path ;
    return $retval;
}
$wpLocation = WP_LOCATION();
define('WP_HOME',$wpLocation);
define('WP_SITEURL',$wpLocation);
define('WP_CONTENT_URL',$wpLocation."wp-content");


Or alternately, you can add the following code, replacing LIVEURL and STAGINGURL with the locations for your live and staging servers:

$currenthost = $_SERVER['HTTP_HOST'];
$mypos = strpos($currenthost, 'localhost');
if ($mypos === false) {
define('WP_HOME','LIVEURL');
define('WP_SITEURL','LIVEURL');
} else {
define('WP_HOME','STAGINGURL');
define('WP_SITEURL','STAGINGURL');
Sources: http://codex.wordpress.org/Running_a_Development_Copy_of_WordPress