How to Move Local Singlesite Database to Remote Multsite Database?

Merging WordPress Standalone Installation with WordPress Multisite Installation

Before You Begin:

  1. Create a new site in your Multisite Network, which will be the site
    you’re migrating from the Standalone installation. (Take note of the site ID #, you’ll need it later.)
  2. You need to make sure that all of the users from the Standalone
    installation, are created and exist in the Multisite installation.

Step 1 – Backup Databases.

Find the directory on your Web Server which the the Standalone copy of WordPress is installed and the directory on your Web Server which the Multisite copy of WordPress is installed and open the wp-config.php file of both installations. Locate the PHP constant DB_NAME. It contains the name of the database that particular installation of WordPress is using.

  1. Backup the Standalone instillation’s database.
  2. Backup the Multisite installation’s database.

Step 2 – Identify the Database Table Prefixes.

By default the database table prefix is wp_.

If you can’t identify the database table prefix just by examining the database. You can look in the base directory of your WordPress installation and open the wp-config.php file of the site in question and look for a line like $table_prefix = 'wp_';.

In your situation, it looks like:

  • The Standalone installation’s database table prefix is the default of
    wp_.
  • The Multisite installation’s database table prefix is custom of
    tenp_.

Step 3 – Export Databases. Import Into Local Environment.

On a local Database Server, create a temporary database for each of these databases. Perhaps to keep things simple, label one database “standalone“, and the second one “multisite“.

  • Import the Standalone installation’s database (which you just
    exported) into the “standalone” database (which you just created) on
    your local Database Server.
  • Import the Multisite installation’s database (which you just
    exported) into the “multisite” database (which you just created) on
    your local Database Server.

Step 4 – Search and Replace.

This is the step where you would likely replace any necessary URL changes. (http to https), (non-www to www), (add or remove directories from URL), etc.

Perform this task on the “standalone” database you created on your local Database Server.

Remember to change things to how you would like them to be in the Multisite installation (the end result).

For this procedure, you’re going to need a Database Tool:

  1. WP-CLI :: wp search-replace which is
    all command line.
  2. Alternatively, if you prefer a GUI, there’s the Database Search and
    Replace Script in PHP by
    interconnect/it
    .

Step 4.2 – Users and Post Authors

You’ll want to probably create a note about all the user’s from your Standalone installation and map the old user ID’s to the new user ID’s (from the Multisite installation).

You can simply just create a temporary text file and do something like this:

1 => 4
8 => 23
15 => 9

The numbers on the left are the ID’s from the Standalone installation and the numbers on the right are the ID’s on the Multisite installation.

You’ll then want to update the post_author column of the wp_posts table to update all of the old user ID’s to the new user ID’s. Otherwise when view your migrated site from Standalone to Multisite, you’re going to be one confused kitten. It’s going to say things on your site were posted by random people and probably even people from different sites in your network. This can be catastrophic if overlooked.

For each of the user ID mappings in your text file, you’ll want to issue a command much like this into MySQL:

UPDATE wp_posts
SET post_author="4"
WHERE post_author="1"
  • The line SET post_author="4" is the new user ID (the user ID from
    the Multisite installation)
  • The line WHERE post_author="1" is the old user ID (the user ID
    from the Standalone installation)

Step 5 – Users and Usermeta

I haven’t really found a good solution for this step yet. I usually just recreate the users manually in the Multisite installation.

In otherwords, I usually just drop two tables wp_users and wp_usermeta.

If anyone would like to improve on this step feel free to edit and add guidance here.

Step 6 – Update Database Table Names

This step is much like Step 4.

You’ll want to map old table names to new table names in the “standalone” database on your local Database Server.

This is the step where you will need to know the site ID # from your Multisite installation.

As an example: If your site ID is 15, and your database table prefix use on your Multisite installation is tenp_, then the database table wp_posts would become tenp_15_posts.

Here’s the MySQL command you can use to update your database table names:

RENAME TABLE `wp_posts` 
TO `tenp_15_posts`;
  • The first line is the old database table name (the database table name from the Standalone installation)
  • The second line is the new database table name (the database table name format to be used in the Multisite installation)

Alternatively, if your database is small enough. You could just export the entire database and open it in a text editor. Then find all & replace. Save it when completed.

Step 7 – Export and Import

Once all of the above changes have been made, export the “standalone” database from your local Database Server.

  1. Import the exported .sql file into the “multisite” database on
    your local Database Server.
  2. Export the “multisite” database from your local Database Server.
  3. Drop the current Multisite database tables used for your existing
    Multisite database (not the one on your local Database Server).
  4. Import the .sql file for your “multisite” database you exported
    from your local Database Server and import it to your database used
    by your existing Multisite installation (the one you just dropped
    all the tables from). Essentially just replacing the current
    Multisite installation’s database with the modified one which
    contains the newly migrated Standalone site.

Moving Uploads files from ./wp-content/.

Standalone and Multisite store uploaded files differently. Multisite stores them in ./wp-content/sites/{$site_id}/. Make sure you move your uploaded files appropriately as well.


Changing the Primary Site:

Look for database table wp_site in your Multisite database. Edit the column id and domain appropriately.

You might need to also edit the site_id column in the wp_blogs table.

Also look in wp-config.php for these lines and once again, adjust them accordingly.

define( 'DOMAIN_CURRENT_SITE', 'www.your-domain.com' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

Useful Links:


End.

If anything is confusing, please comment and I’ll try to clear it up.