I’ve been using the fabulous WP Migrate DB Pro since June; it makes it really easy to duplicate the data from one WordPress website onto another, something that developers need to do frequently, and it handles the problems of moving serialised data from one server to another without breaking it. When pulling data from a production server to a development or test environment, it also (by design) replaces all your settings, which might mean that test emails go to your clients — can anyone say, “Dear Rich Bastard?” Thankfully, it also offers a couple of save-your-backside solutions.
Firstly, yes, it really is necessary that WP Migrate DB Pro clobbers your test settings by default. After all, you are pulling data from Production for a reason — you want to have a test or development environment that is sufficiently like Production that you can replicate any reported problems and resolve them properly. You just need to be careful to put back a few select settings so that you don’t send a deluge of test emails to your client, submit test transactions to their live payment gateway account, or populate their MailChimp lists with Mr Test Only and his friends.
Secondly, I highly recommend you use carefully worded inoffensive test data when testing, just in case you miss something — Mr. Test Only of 123 Example Street, Sometown is clearly an obvious test, and unlikely to get you in as much trouble as Mr. Rich Bastard apparently can if things go awry.
But getting back to WP Migrate DB Pro, there are a couple of handy hooks that you can utilise to keep your dev or test environment settings when pulling data from Production. The first and most universally useful one is the wpmdb_preserved_options
filter, which lets you tell WP Migrate DB Pro that you want some settings in wp_options left alone! Here’s a basic example:
/** * tell WP Migrate DB Pro to preserve some options, * so that we can stay in dev / test mode * @param string $preserved_options * @return string */ add_filter('wpmdb_preserved_options', function($preserved_options) { $preserved_options = array_merge($preserved_options, array( 'active_plugins', // don't clobber dev/test plugins! 'admin_email', // don't send test emails to client! 'blog_public', // don't tell Google it can index test site! )); return array_unique($preserved_options); });
That will let you preserve all the simple settings in wp_options, but not everything is that easy. Many plugins store compound data structures (arrays, objects) serialised in a single WordPress option. Often, you want to get those settings from Production, but preserve some specific sub-settings — e.g. the fact that you are using the payment gateway in test / sandbox mode, and the special testing client ID it needs. Rather than preserve the whole compound setting, you can use the wpmdb_migration_complete
action hook to reset those sub-settings after the migration has completed. Here’s an example I use for WooCommerce with my eWAY Payment Gateway add-on:
/** * after WP Migrate DB Pro migration * @param string $type * @param string $location */ add_action('wpmdb_migration_complete', function($type, $location) { // only for Pull migrations from another server if ($type == 'pull') { // force refresh of options wp_cache_flush(); // WooCommerce eWAY settings $settings = get_option('woocommerce_eway_payments_settings'); if (is_array($settings)) { $settings['eway_customerid'] = '87654321'; $settings['eway_sandbox'] = 'yes'; update_option('woocommerce_eway_payments_settings', $settings); } // WooCommerce email settings $settings = get_option('woocommerce_new_order_settings'); if (is_array($settings)) { $settings['recipient'] = ''; update_option('woocommerce_new_order_settings', $settings); } } }, 10, 2);
I’ve wrapped those two snippets up in a simple plugin that I can drop into any of my WooCommerce websites when I’m working on them. You can download it from this gist, and modify to suit your own purposes.
With WP Migrate DB Pro and a couple of nifty hooks, job is done over and over without the hassle of forever manually resetting the dev environment settings.
Edit: Chris over at Delicious Brains has written a post introducing these two hooks and others; check out Tweaking WP Migrate DB Pro with Actions and Filters and join the discussion.