How can I work on a database along side a client?

You’ll need to make some modifications to wp-config.php.

Add the following so WordPress picks up the domain automatically based on the environment:

define( 'WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] );
define( 'WP_HOME', 'http://' . $_SERVER['SERVER_NAME'] );

Now we need to make WordPress a little more portable by having it determine which host to use based on which environment your in. There are several ways to do this, but essentially all you need to do is add a conditional statement so WP knows if you’re in your development or production environment. I usually use *.dev domains for my dev environment so…

define( 'DB_NAME', '' );
define( 'DB_USER', '' );
define( 'DB_PASSWORD', '' );
define( 'DB_HOST', ( strpos( $_SERVER['SERVER_NAME'], '.dev' ) === false ? 'localhost' : 'db.domain.com' ) );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );

Now that we can connect to the database we have a working version of the site, but there’s a caveat: absolute links in pages/posts. You’ll be able to navigate the site just fine using links that use WP functions to determine the address (like the nav, bloginfo(), etc..), but any images that have been inserted into a post will have the full URL of the production site. Technically, you can stop here, but if you want to be able to use both versions of the site there’s some more work to be done.

First, add the following to ‘wp-config.php`:

define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/wp-content' );
define( 'WP_CONTENT_URL', '/wp-content' );

This tells WP to use relative image paths instead of absolute. For example: /wp-content/uploads/image.png instead of http://domain.com/wp-content/uploads/image.png

This change isn’t retroactive though, so you’ll need to run a find and replace on the database to catch these. Be careful of serialized arrays in the db though- a standard find and replace won’t work so you’re best off using something like this: https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

We still have links stored within pages/posts with absolute URLs though unless you take special care when inserting them to not include the domain. You can remove the ones already stored using the search and replace tool a mentioned earlier.

Finally, we need to make files portable between environments. You can use something like rsync or hook into the add_attachment filter to send uploads made in dev to production or a CDN if you’re using one.

I recited this from memory, so hopefully I didn’t miss anything. 🙂