DEV Community

Matthew Dailey
Matthew Dailey

Posted on

Using WP-CLI and Composer to migrate a WordPress site

In a recent project, I had to convert a single WordPress site to a multisite for a client. The approach I took was to create a new multisite install locally and import the content from the existing database. With the newly created WordPress multisite up and running locally on my computer, the next step was to migrate the site to a subdomain.

Migrating a WordPress multisite from localhost to a test site can feel daunting but this is where WP-CLI (WordPress command-line) and Composer can save a considerable amount of time. I maintain my plugins using Composer. Rather than commit all of my plugins to a Git repository I list the plugins in a composer.json file and install them from WordPress Packagist, a composer repository for themes and plugins. Only custom or propriety themes and plugins are committed to a Git repository.

Using both WP-CLI and Composer you can save time from having to upload plugins and WordPress core files. Using SSH access, go into the folder on the server where the WordPress install will reside. Since I'm on Windows I used Putty for SSH access to the server. Run the wp-cli command: wp core download. If you don't have WP-CLI installed, go to the documentation for instructions on how to install. This command will download the latest version of WordPress. Next run this command: wp config create --dbname=testing --dbuser=wp --dbpass=securepswd to create a wp-config file. Make sure to put in your actual database credentials. Or you can upload your existing wp-config file and change the database credentials. With a multisite the install command is slightly different. The command would be as follows:

wp core multisite-install --title="Welcome to the WordPress" --admin_user="admin" --admin_password="password" --admin_email="user@example.com"

Import your database into the server. I'm not going to go into detail on how to do this as it it's outside the scope of this post. The URLs in the database need to be changed. Using WP-CLI, run command: wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid --skip-tables=wp_users This will replace the localhost urls with the live server urls but skip the users table and any entries that have a guid.

We will use Composer to install all plugins and themes at once. Move the composer.json file to the server if it's not already there. Using SSH access, run 'composer update' while in the WordPress folder on the server. This will install all the plugins and themes listed in the composer.json. You can use FTP to move over any custom plugins or themes not listed in the composer.json. You now have migrated a WordPress multisite.

There are steps I purposely left out, like moving over images or importing a database. I wanted this to be a general overview. Hope you found it helpful. For a list of WP-CLI commands check out this page.

Top comments (0)