Run WordPress Locally. No need to modify the DataBase!

Simply add the following lines to your “wp-config.php” and you’ll be able to run your same WordPress code and use your same WordPress database on both your live server and your local development server.

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;
}
define('WP_HOME',WP_LOCATION());
define('WP_SITEURL',WP_LOCATION());

If you use BBpress, you can so something very similar, but edit the bb-config.php

function WP_LOCATION () {
    $script_path = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
    $bb_base_path = realpath(dirname(FILE) . DIRECTORY_SEPARATOR . '..');
    $web_subfolder = substr( $script_path, strlen($bb_base_path));
    $retval = 'http' . ($_SERVER['HTTPS'] ? 's' : null) . '://' . $_SERVER['HTTP_HOST'] . $web_subfolder ;
    return $retval;
}
$bb->uri = WP_LOCATION();

I’ve updated the WordPress documentation under Running Development Copy of WordPress to note the discovery.

If manually running SQL updates make you feel happy and productive, then you may prefer running the manual database update on your local development system each time you copy the database off live. For your reference, the SQL command to do the trick is:

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";

Don’t forget to change local_folder_name to you’re actual local WordPress path. To make development simpler, I recommend updating your /etc/hosts and adding aliases for your local sites. For example:

127.0.0.1 localhost XYZproject.local PDQproject.local otherProject.local

Then setup separate VirtualHosts for each of your projects, and access them with the alias defined in your hosts file.

3 thoughts on “Run WordPress Locally. No need to modify the DataBase!”

  1. Whole Disk Encryption Each day, millions of people go to online to work, do homework, purchase products, manage devices in their homes, and connect with friends. Technology such as the big information are modifying our everyday life at a quick speed, while exponentially increasing the quantity of private data that is attained, used, and shared.

  2. Whole Disk Encryption Each day, millions of people go to online to work, do homework, purchase products, manage devices in their homes, and connect with friends. Technology such as the big information are modifying our everyday life at a quick speed, while exponentially increasing the quantity of private data that is attained, used, and shared.

Leave a Reply

Your email address will not be published. Required fields are marked *