Working with WordPress locally or on a development server


Quick tip for those who develop in a local, or development, LAMP/XAMPP test environment. Working on WordPress can be a stinger, because of its tendency to use absolute URLs.

Having tried a bunch of super quick fixes (mostly rewrites and greasemonkey scripts), I think I’ve got one that should help.

Since most of those absolute URLs are created using a couple of the blog options, namely siteurl and home, we just need to update those WP options if it detects that we’re on our local or development environment.

For example, let’s say that we use the host name 10am.ca.localhost when working on our local site.

Edit (or create) the functions.php file in your theme directory:

<?php
/**
* Update the WordPress blog options siteurl and home if working on localhost.
* Author: PJ (pj [at] 10am.ca)
*/
if( ! preg_match('@.localhost@', get_option( 'siteurl' ) ) ) {
	update_option( 'siteurl', preg_replace('@(https?://)([^/]+)(.*)@', '12.localhost3', get_option( 'siteurl' )) );
	update_option( 'home', preg_replace('@(https?://)([^/]+)(.*)@', '12.localhost3', get_option( 'home' )) );
}