0

I was wondering what the best way would be to work on a database locally while a client is placing content into the database externally.

My initial thought was to connect remotely to the database but the sitter is set to the development website and not http://localhost, which is what I use. It connect fine but as I navigate through the website, the link changes to match that of the development server.

1
  • 1
    There is no easy way to work with 2 different databases to later on merge them. I often request the client to stop making updates during time of development or to keep track of the updates that were made. The database shouldnt be a big concern if you're only making changes to the theme files, but any changes on the back-end of wordpress won't be too easy to sync. I have seen some paid plugins that help you sync the database.
    – gdaniel
    Commented Feb 3, 2015 at 13:29

1 Answer 1

0

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. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.