Temporary WordPress URL

Sometimes you need to move your website to a temporary address. For me, this is usually for development. The best option is to put in a hosts rule to the computers that need to access the site under the domain, but this is not always an option for everyone.

There are numerous posts about adding these two lines to your wp-config.php file


define('WP_HOME','http://NEW_URL');
define('WP_SITEURL','http://NEWURL');

This is a good start, but any embedded links or images will go to the URL that was defined when the link was created. This is mainly in page content.

To get around this you can use output buffering.
In the functions.php file for your theme add in the following code


function callback($buffer) {
$buffer = str_replace('OLD_URL','NEW_URL',$buffer);
return $buffer;
}

function buffer_start() { ob_start(“callback”); }
function buffer_end() { ob_end_flush(); }

This does a string replace of any instances of the old url, with the new url before the page is sent to the browser.
I would only recommend this for a temporary option while you are doing development or testing.

The long term fix is to update the url’s in your database.

You can do this by dumping your current database. Run sed or similar over the file, replacing the URL, then importing the updated database.