Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I performed a MySQL dump of my WordPress production database to be imported into my local development environment. I plan on frequently performing this database dump and import. Is it possible for me to AVOID running a search & replace script or a MySQL query that updates all of the permalinks, site URLs, etc?

Could I include something like the following in my wp-config.php to override the above said links:

define('WP_HOME',    'http://localhost.com:8888/example');
define('WP_SITEURL', 'http://localhost:8888/example');

My goal is to make it as EASY as possible to perform routine database dumps from my production site to local.

share|improve this question
add comment

2 Answers 2

What you posted will work fine for all links that are generated by WordPress: permalinks, script/style enqueues for local files, featured images, etc.

I tend to define my Site URL and Home URL dynamically like this:

<?php
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', WP_HOME . '/wp');

Note: HTTP_HOST isn't always present, use with caution

As far as in content, links and urls -- things like links to other posts or links to images, those will not get changed as they are just in the database, not generated dynamically. You can probably do some preg_replace magic with the the_content filter, however.

<?php
// probably too simplistic and will break things.
add_filter('the_content', 'wpse75106_change_host');
function wpse75106_change_host($c)
{
    return preg_replace(
        '#(https?://)production_site.com#u',
        '{$1}local_site.com',
        $c
    );
}

That said, the important things are the stuff WP generates, I wouldn't worry too much about in content links/images on your local machine.

share|improve this answer
add comment

Naive search and replace is simply not good enough, as part of the data in the DB (widgets for example) is serialized and in order to replace text in a serialized data you first have to unserialize it.

The best thing you can do is to fully duplicate the production enviroment on your development server which should include at the minimum changing your hosts file to point to production domain to your testing server IP address. Once you set this up, keeping your source files and DB in sync should give you a good development environment.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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