Convert WordPress Single Site to Multisite Subsite with WP-CLI and bash

Converting a WordPress single site to a Multisite subsite can really test your patience levels, especially if you are unfamiliar with the multisite structure. Previously I have shown how to convert a Multisite subsite to a single site. This guide walks you through step by step to convert a WordPress single site to a multisite subsite using WP-CLI and bash tools.

If it any point you struggle with this and want a WordPress single site converted to a multisite subsite for you then feel free to get in touch on Codeable.

Convert WordPress Single Site to Multisite Subsite with WP-CLI and bash

Here in this hypothetical example the single site is guides.wp-bullet.com stored in this path

/var/www/guides.wp-bullet.com

I want to convert the guides single site to a subsite for the multisite wp-bullet.com with this path

/var/www/wp-bullet.com

Create a temporary folder to store the single site converted to subsite

mkdir -p /tmp/wp-bullet.com/

Prepare Multisite for New Subsite

Enter your multisite root, usually in a similar path to below, remember to change your multisite path (wp-bullet.com)

cd /var/www/wp-bullet.com

Before getting a list of all subsites here is the basic structure of the folders

InfoSubsite 1 ValuesSubsites > 1 Values
blog_id12
Database prefixwpb_wpb_2_
Plugins folderwp-content/pluginswp-content/plugins
Themes folderwp-content/themeswp-content/themes
Uploads folderwp-content/uploads
wp-content/uploads/sites/2/

Create a new subsite with WP-CLI https://developer.wordpress.org/cli/commands/site/create/

wp site create --slug=guides

You will get this formatted output below which provides critical info for converting the WordPress single site to subsite correctly.

Site 2 created: http://guides.wp-bullet.com

Get some display information about your multisite installation

wp site list

The blog_id is 2 which the database prefix is made up for the url value guides.wp-bullet.com subsite

+---------+------------------------------------------+---------------------+---------------------+
| blog_id | url                                      | last_updated        | registered          |
+---------+------------------------------------------+---------------------+---------------------+
| 1       | http://wp-bullet.com/                    | 2017-02-14 16:09:14 | 2017-01-31 11:35:34 |
| 2       | http://guides.wp-bullet.com/             | 2017-02-21 21:30:15 | 2017-01-31 11:37:04 |
+---------+------------------------------------------+---------------------+---------------------+

Putting it together the full database prefix for the new guides subsite is wpb_2_

Extracting Database from Single Site and Converting to Subsite

WP-CLI allows you to export database tables specific to each subsite by simulating that the request came from the subsite’s URL.

Enter your single site installation, here /var/www/guides.wp-bullet.com

cd /var/www/guides.wp-bullet.com
wp db export --skip-plugins --skip-themes /tmp/singlesite.sql --allow-root

Get the WordPress database prefix from the single site  wp-config.php with grep

grep "table_prefix" wp-config.php

Here wpb_ is the database prefix

Take note of your database prefix here! It will be used in future steps
$table_prefix = 'wpb_';

In case your multisite uses a different database prefix make sure to get that one as well

grep "table_prefix" /var/www/wp-bullet.com/wp-config.php

The subsite database prefix is made up of the multisite prefix wpb_ and the blog_id (subsite id) 2 for the wp-bullet.com multisite’s subsite guides.wp-bullet.com.

This sed command will replace the database table names replacing the single site prefix with the multisite prefix and blog_id then create a new .sql file

sed "s#wpb_#wbp_2_#g" /tmp/singlesite.sql > /tmp/guides-subsite.sql

If you are copying the database – regardless of your blog_id – to a remote location you can gzip it so it’s much smaller

gzip /tmp/wp-bullet.com/guides-subsite.sql -c > /tmp/wp-bullet.com/guides-subsite.sql.gz

Packaging WordPress wp-content Folder

We need to compress these folders and extract them to the new multisite installation later

  • WordPress plugins
  • WordPress themes
  • WordPress wp-content uploads which contains images, videos etc for that subsite

Compressing the plugins is done from the multisite root folder wp-bullet.com

tar -czf /tmp/wp-bullet.com/guides-plugins.tar.gz -C /var/www/guides.wp-bullet.com/wp-content/plugins/ .

Similarly, compressing the themes folder is done from the multisite root folder wp-bullet.com

tar -czf /tmp/wp-bullet.com/guides-themes.tar.gz -C /var/www/guides.wp-bullet.com/wp-content/themes/ .

The wp-content uploads folder can be packaged using this tar command

tar -czf /tmp/wp-bullet.com/guides-uploads.tar.gz -C /var/www/guides.wp-bullet.com/wp-content/uploads/ .

Now you have 3 tar files containing the essential components for your WordPress multisite subsite.

  • /tmp/wp-bullet.com/guides-plugins.tar.gz
  • /tmp/wp-bullet.com/guides-themes.tar.gz
  • /tmp/wp-bullet.com/guides-uploads.tar.gz

The database has both an sql and gzipped version

  • /tmp/wp-bullet.com/guides-subsite.sql
  • /tmp/wp-bullet.com/guides-subsite.sql.gz

These tars will be extracted to the single site in the next section.

Restore Converted Single Site to Subsite

We need to perform the following tasks to make the converted single site to a multisite subsite

  • Import the WordPress database from single site to subsite
  • Extract wp-content tars from single site to subsite
  • Fix permissions if necessary

Import WordPress Database from Single Site to Subsite

Enter multisite installation with your WordPress files, here it is wp-bullet.com.

cd /var/www/wp-bullet.com

Import database for the subsite and import it into the WordPress single site installation

wp db import /tmp/wp-bullet.com/guides-subsite.sql --url=guides.wp-bullet.com

If you happen to have additional problems you may want to read the advanced database search and replacement guide for WordPress.

Extract wp-content to Subsite

Extract the themes.tar.gz to your new single site installation

tar -xf /tmp/wp-bullet.com/guides-themes.tar.gz -C /var/www/wp-bullet.com/wp-content/themes/

Next, extract the plugins.tar.gz to your new single site installation

tar -xf /tmp/wp-bullet.com/guides-plugins.tar.gz -C /var/www/wp-bullet.com/wp-content/plugins/

Finally, extract the uploads.tar.gz to your new single site installation

The wp-content uploads folder is specific for the subsite using 2 as your blog_id (subsite id) for the 2 subfolder in the uploads/sites folder

tar -xf /tmp/wp-bullet.com/guides-uploads.tar.gz -C /var/www/wp-bullet.com/wp-content/uploads/sites/2/

Fix permissions, if www-data isn’t the owner’s user and group change them to the correct values.

sudo chown -R www-data:www-data /var/www/wp-bullet.com
sudo find /var/www/wp-bullet.com -type f -exec chmod 644 {} +
sudo find /var/www/wp-bullet.com -type d -exec chmod 755 {} +

The last thing to do is handle the users tables as outlined by Delicious Brains.

Sources

tar Directory without Storing Absolute Paths
WP-CLI db tables command
Delicious Brains – Converting Single Site to Multisite Subsite