Promoting Subsite to Primary in WordPress Multisite

A buddy of mine was going through the process of rebuilding his website in WordPress and asked for some pointers. I applaud him for the design as I found his local version of the site pretty awesome. Zak is a video editor and has a job that I couldn’t do, working for Nike, EA, and a lot of other big names in the industry. The videos are impressive and can be seen on his website.

WordPress Multisite over Local Development

Zak was developing his site on his local laptop using XAMPP. While I don’t have a problem with this, it means that you are going to have a huge learning curve if you don’t work in WordPress all the time, and you’re ready to move it. Using this method, you will need to back up the database, move your local WordPress installation to the server (likely overwriting your existing install), and then finally hope you get the tricky permissions right to update the core plugins and themes.

I suggested instead to develop the site on his existing website after converting his existing WordPress installation into a Multisite installation. This configuration is straightforward, even if you don’t have direct access to your server and only have a UI, like CPanel, available to you. So easy that Zak did that part on his own. I won’t get into how you can set up Multisite because others thoroughly document it.

There are plenty of WordPress devs that won’t like my advice here, but this is how I do my sites. This very instance has ten sites running on it, and you as the reader would never know that. For me, being able to start the process of building an idea into a site quickly is fantastic and a necessity. I tend to reuse the same plugins, themes, etc., and a multisite makes that easier for me. We chose to use this method because it works, and it’s simple.

10 Steps to execute Primary to Secondary and Vice Versa

  • Create A Subdomain
  • Update the siteURL to use this subdomain
  • Create a backup of your MySQL database (PHPMyAdmin)
  • Download the MySQL database backup to your local machine
  • Update the SQL database in a text editor locally
  • Create a new empty database in PHPMyAdmin or CPanel
  • Add the same users from the primary database to the new database
  • Import your edited backup to the new database
  • Update the wp_options tables and then update your wp_blogs table
  • Update your wp-config.php to point to the new database, and your sites, 1 and 2 will have been flipped. The secondary is now the primary site; the primary is now the secondary

Create a WordPress Subdomain and Update SiteURL

After Zak had added another site to his existing WordPress install and built his site, he was ready to make the flip from his old site and his new site. We did a little planning and made a subdomain, archive.zakinzinger.com, and created a DNS A record for archive and pointed it to the same domain. Our old site would be archive and our new site would be the primary.com

Summarizing, archive.zakinzinger.com would temporarily point to the new site in WordPress. The old site would continue to look to zakinzinger.com. We chose to update the siteURL for the new site in WordPress under My Sites, Network, Sites to the archive domain. After a couple of hours, DNS propagated, and we were able to access the subdomain successfully

First off, let me say that this should be a much easier process for WordPress users, but the way we completed this task took about 5 minutes after thinking it through. If you have followed along this long, it may be worth following the rest. Remember, we are making no changes that cannot be undone, and that will remain the case too.

Backup and Download current MySQL database (PHPMyAdmin)

In our case, I didn’t get SSH access until after we had done most of the work. I was only able to leverage the tools in CPanel, and I have to be honest, I am quite impressed with the ability to find and use the tools provided. It’s been ten years since I have had to use it, bravo to the teams that support and build that product, it’s come a long way.

In CPanel, you will need to know the name of the Database being used before you can back it up and download it. The easiest way to do this is to reference the wp-config file in the WordPress installation directory, generally in the public_html folder on shared hosting plans. I accessed the data by using the built-in File Manager. Highlight the file and choose edit from the submenu at the top.

When you open this file, you will see a line identifying the database name, as well as, the user with access to it. Go ahead and copy this text somewhere, we will edit the database name later.

define('DB_NAME', 'wp_database');
/** MySQL database username */
define('DB_USER', 'wp_username');
/** MySQL database password */
define('DB_PASSWORD', 'wp_password');
/** MySQL hostname */

Back in CPanel, you should have the ability to access MySQL Databases, hopefully with PHPMyAdmin. If you have SSH access, this is going to be super easy as long as you have a MySQL username and password, but I am going to cover the process of doing this in CPanel. In PHPMyAdmin, choose the database from the wp_database entry you found above, and then select the export option to the right. Choose the Custom option and export the backup, accepting all of the defaults except for compress as zipped. The file will now download to your local computer

Update the SQL database in a text editor locally

We have the database and as complicated as a database can be, it can also be edited as a plain text file in this case. I am using Sublime Text and will spend some time doing a simple find and replace to change the URLs being used in the database, content paths, and siteURLs. We are almost done believe it or not. Where the database name field is located in this backup, go ahead and change the name to one that makes sense for the database. We will create it later. We want to make sure we are consistent.

Credit where credit is due, the next set of steps are taken from a StackOverflow page, but referencing that page could/would only confuse the change we are about to make because we are doing it differently. Stackoverflow Link

At the moment, all URLs in the content tables are still pointing to the resources of the old sites.: Replacing every https://archive.zakinzinger.com with http://zakinzinger.com and every http://zakinzinger.com with http://archive.zakinzinger.com in the next step will end with all former URLs pointing to site ID 1 (http://zakinzinger.com).

The best way would be to insert an intermediate step:

  • Search for https://archive.zakinzinger.com and replace it with a preferably unique slug: https://change.example.com
  • Search for https://zakinzinger.com and replace it with https://archive.zakinzinger.com
  • Search for http://change.example.com and replace it with https://zakinzinger.com

Save your SQL file as the database name you named above, in my case, it would be wordpress_tutorialdocumentation.sql

Create a new empty database and add users in PHPMyAdmin or CPanel

In CPanel, visit the database option, and choose to create a new database, give it the same name you defined above. In our case, it will be wordpress_tutorialdocumentation. You should have the option in CPanel to add a user to the database, and we are lucky because we copied the username above we need to grant access to the database. Grant all access when you click Add.

Import your local SQL file into your empty, new database

From Cpanel, you can choose the PHPMyAdmin option again. Choose your newly created database from the left menu by clicking directly on the name of it. Then in the top list, choose the import option. It will take some time to import, be patient.

We have changed nothing on the running site. The database we just created is not being used for anything, and that’s a good thing because we have to make a couple of changes while we are here and before we make the switch from the primary WordPress site to the secondary and vice versa.

Update the wp_options and wp_blogs tables

WordPress uses the tables wp_*_option and wp_blogs to find the matching blog for a given request URL and to build proper permalinks for this blog. So we have to change the values in the following three tables (for this example):

In wp_options the keys home and siteurl
In wp_2_options the keys home and siteurl
In wp_blogs the column domain for both sites with ID 1 and 2

In wp_options (the options table for site ID 1) I changed the values of both keys home and siteurl from https://zakinzinger to http://archive.zakinzinger.com.

I did exactly the same as the table wp_2_options but here I changed the value from http://archive.zakinzinger.com to http://zakinzinger.com

Next step is to update the table wp_blogs:

Here you simply switch the values from both sites in the domain column:

  • zakinzinger.com becomes archive.zakinzinger.com and
  • archive.zakinzinger.com becomes zakinzinger.com

Update your wp-config.php to to the new database

Now you have to update the wp-config.php to deal correctly with the new settings data:

const MULTISITE            = TRUE;
const DOMAIN_CURRENT_SITE  = 'wp.tmp';
const PATH_CURRENT_SITE    = '/';
const SITE_ID_CURRENT_SITE = 1;
const BLOG_ID_CURRENT_SITE = 2; // This is the new root site ID
const SUBDOMAIN_INSTALL    = TRUE;

We also want to point this WordPress install to another database, remember the settings from above?

//define('DB_NAME', 'wp_database'); 
//leaving this here but commented to ignore
define('DB_NAME', 'wordpress_tutorialdocumentation');
/** MySQL database username */
define('DB_USER', 'wp_username');
/** MySQL database password */
define('DB_PASSWORD', 'wp_password');
/** MySQL hostname */

You’ve created a new database with updated URLs, with appropriate users and permissions, and with no downtime. As soon as you save this wp-config.php change, your site will have changed from site 1 to site 2, and 2 to 1.

Let me know in the comments if you’d like to see me add instructions on making these changes directly from the command-line via ssh and I will be happy to revisit the article.

Leave a Reply

Your email address will not be published.