Tomoro Logo

Multisite Domain Mapping and WP DB Migrate Pro

Did you know you don’t have to use a plugin for domain mapping? Since WordPress 4.5! And that’s good news because domain mapping plugins are pretty terrible. In your Dashboard, edit the site you want to give a custom URL and change the Site Address (URL) field to a domain already pointing at your hosting.

Ideally in your Development environment you want your sites to work on subfolder domains:

1 example.dev
2 example.dev/secondsite

Then on the Staging/Production environments:

1 example.com.au
2 secondsite.com.au

But there’s a catch, if you’re a heavy user of Migrate DB Pro (and you should be), it will just give you this after a Push migration:

1 example.com.au
2 example.com.au/secondsite

Or worse, this after you Pull:

1 example.dev
2 secondsite.com.au

Which isn’t ideal! You then need to go back and re-map the domains in the Dashboard after every migration.

With the help of the additional Tweaks plugin we can write some code to modify the relevant tables once the migration is complete. So let it swap your .dev to .com.au and then fix the rest.

Code!

Add this code into the Tweaks plugin file inside the migration_complete function, and uncomment that function from the top of the file. This code runs at the end of a migration, on both databases. Most importantly, it runs after the find/replace.

The only customisations you should need to make are:

  • The protocol – I’m using https://
  • The TLD’s – I’m using .com.au on Production and .dev on Development

On Development, this code will:

  1. Get the domain name of blog #1 (the ‘root’ blog)
  2. Go through each blog after that
  3. Change the mapped domain name to the root blog domain
  4. Set the subdirectory folder to the name of your production domain
  5. Update each blog’s Options to use this new URL

On Production, this code will:

  1. Go through each blog, other than the ‘root’ blog
  2. Remove the slashes from the subdirectory folder name, and turn it into a domain name
  3. Use that domain name for that blog
  4. Update each blog’s Options to use this new URL

Notes:

  • Thanks to Delicious Brains for pointing me in the right direction to make this, but yeah it’d be nice if the Multisite Add-on plugin handled this natively.
  • The migration_complete function runs on both databases after the migration is complete. So I have written this such that the same version of the Tweaks plugin can be present on both development and production.
  • My code was based on this script which was for updating site URLs after doing a migration.
  • This was written with subdirectory multisite installs in mind. No idea what you’d need to do with subdomains, other than take my advice that you should not use subdomains for multisite.
  • I’m no PHP/SQL wizard, it’s not a stretch for me to imagine this code could be written better.
  • Test this first, don’t just go using it on your Production environment. Please.