WordPress Dev Environment URL Helper

Tim moves our rampages database to our dev server roughly every month. Our database is big and awkward and uses Multi DB to deal with the scale. Because of the multiple databases some of the typical migration patterns to get the URLs right don’t work. We were suffering through that for a while but I’d find myself ported from the dev environment to the live site at times without realizing that’s what happened. That is not a fun way to live.

Because Jeff was working on a really smart way to do this in the database I realized I didn’t know more than I thought I didn’t know. As a result I stumbled upon the ability to filter the site url and home options. That means we can make a tiny little network activated plugin that leaves our database alone and just filters things on the fly with a dab of regex.

function replace_siteurl($val) {
	$clean = preg_replace('/OLD_URL_THING/', 'NEW_URL_THING', $val);
    return $clean;
add_filter('option_siteurl', 'replace_siteurl');
add_filter('option_home', 'replace_siteurl');