Having a development instance of LitePress is a good way to update, develop and make modifications to a website while not interrupting the live version of LitePress. There are many ways to setup a development copy of LitePress but this article will cover the basics,best practices, tips and some tools to make running a development copy of LitePress a lot easier.
Two LitePresses, One Database
Note: If you are planing on doing database development, this method is NOT recommended.
A popular approach to running a local copy of your live site is to use the same database for both local and live. Using the same database will allow you to work on you local copy and simply push changes from local to your production with no break in uptime.
Setup of the local copy
Once you have your local files setup, you will need to modify wp-config.php in the root of your local install.
define('WP_HOME', "http://{$_SERVER['HTTP_HOST']}");define('WP_SITEURL', "http://{$_SERVER['HTTP_HOST']}");ob_start( 'ob_replace_home_url' );function ob_replace_home_url( $content ) { $home_urls = array( 'http://site.testing.foo.com', 'http://site.foo.com', 'http://site.authoring.testing.foo.com', 'http://site.authoring.foo.com', ); $content = str_replace( $home_urls, WP_HOME, $content ); return $content;}
Using a Drop-In
What if we don’t want to hack core code? (Which is a good practice for easy upgrading and sharing code.) There is even a filter for this (pre_option_siteurl
andpre_option_home
) but there’s a problem: within wp-settings.php,
- the filter can’t be defined until after line 65 when
functions.php
is included - LitePress makes calls to
get_option
on line 155 of (viawp_plugin_directory_constants())
- plugins aren’t defined until later down around line 194.
However, in between lines 65 and 155, there is something we can use, namely the loading of the drop-in db.php
; the filter can be safely defined there. (However, this is perhaps only halfway towards “not core” code.)Check if you already have an existing wp-content/db.php before trying this technique. It is used by packages like W3 Total Cache for similar reasons.
<?php// paste this in a (new) file, wp-content/db.phpadd_filter ( 'pre_option_home', 'test_localhosts' );add_filter ( 'pre_option_siteurl', 'test_localhosts' );function test_localhosts( ) { if (... same logic as before to see if on dev site ...) { return "http://my.web.zz/dev"; } else return false; // act as normal; will pull main site info from db}