Using WP-CLI and Composer to migrate a WordPress site
Tags: wordpress, composer
Published: 2020-07-06 19:25:33


<p>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. </p>
<p>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 <a href="https://wpackagist.org/">WordPress Packagist</a>, a composer repository for themes and plugins. Only custom or propriety themes and plugins are committed to a Git repository. </p>
<p>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 <a href="https://www.putty.org/">Putty</a> for SSH access to the server. Run the wp-cli command: <code>wp core download</code>. If you don't have WP-CLI installed, go to the <a href="https://wp-cli.org/">documentation</a> for instructions on how to install. This command will download the latest version of WordPress. Next run this command: <code>wp config create --dbname=testing --dbuser=wp --dbpass=securepswd</code> 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:</p>
<p><code>wp core multisite-install --title="Welcome to the WordPress" --admin_user="admin" --admin_password="password" --admin_email="user@example.com"</code></p>
<p>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: <code>wp search-replace 'http://example.com' 'http://example.dev' --recurse-objects --skip-columns=guid --skip-tables=wp_users</code> This will replace the localhost urls with the live server urls but skip the users table and any entries that have a guid. </p>
<p>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.</p>
<p>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 <a href="https://developer.wordpress.org/cli/commands/">page</a>.</p>