How WP-CLI Can Make Your Life Easier (Video)
Recently we had the pleasure of hosting a webinar with Alain Schlesser, the lead developer of the WP-CLI project, the command line interface for WordPress. Nearly 1300 people attended and we had a blast fielding questions and seeing our clients engage with us live. Even if you have missed the live stream, it is not too late to learn how to do some of the most popular things with WP-CLI, like automating backups, troubleshooting website errors, performing a search & replace in your database, cleaning up spam comments, resetting your passwords, and more. All you have to do is watch the webinar video below.
You can find a list of all commands Alain used in the webinar here.
A lot of users’ questions were answered by Alain during the live webinar. However there were some that we couldn’t get to and we’ve added them below along with the answers.
Q: Are changes made by WP-CLI on a development site carried forward when the site is migrated and made live?
A: Changes made by WP-CLI are applied to the WordPress installation which resides in the folder you’re in. Alternatively, you can specify a ‘–path’ to the installation you want to work with. If you execute commands to your live site, e.g. in your public_html folder, they will immediately apply to it.
Q: What can WP-CLI do that I can’t do just as easily by running SQL against the tables?
A: Using WP-CLI is much easier, faster, and most importantly safer than running SQL directly. Plus, it takes care of things like serialization, lowering the chance of creating errors.
Q: Is it possible for me to transfer WooCommerce orders between multiple sites creating a multisite setup using WP-CLI?
A: There is no default command that can do that. There are custom commands and workarounds but we recommend you use the export functionality of WooCommerce in the background for this purpose.
Q: How could we force a WordPress site to use a remote database? For example, on SiteGround, with content and database, we wanted the site to use a remote database for its wp-config on Digital Ocean or some other server, can it be done with WP-CLI using SSH?
A: Yes, this is doable but you don’t need WP-CLI for this task. All you need to do is edit your wp-config.php file and replace the MySQL hostname, port, user, password and db name configuration lines with the ones from your other server. Once WordPress links to it, WP-CLI will work with the remote database just fine.
Q: Can we do content and db backups to a remote server on a schedule?
A: Yes, you could do that. You can set up a cron job from your hosting account control panel to call wp db export. Then, you can upload that file to a remote server. This, however, would require another bash script depending on the access you have to the remote server and the level of authentication it requires.
Q: Can you install a theme from GitHub?
A: Yes, the ‘wp theme’ command accepts URLs as a parameter.
Q: Is there any way to force check for updates like the admin UI does? So many times WP-CLI won’t show updates when there are updates ready.
A: That’s a great suggestion for a new command! I recommend joining the #cli channel in the https://make.wordpress.org Slack!
Q: Is there a way to have a pre-configured wp installation package with theme and theme settings, plugins, and SEO settings in place so I can quickly create websites in my cPanel?
A: Yes, you can do a simple script as the one we showed in the beginning of the webinar that installs everything you need and uses the ‘wp option update’ command to configure your SEO and other plugins.
Q: When installing WP-CLI with a local environment, would the install be in the root of the computer (global) or the root of the site?
A: Where you install it locally depends solely on your personal preferences.
Q: Can I only use WP-CLI if the server or host accepts ssh?
A: Yes, it’s a command line interface which requires shell access.
Q: What about reversing mistakes . . . is there a reversion command?
A: WP-CLI doesn’t track the changes you make. This said, if you back up before making changes, you can easily restore from that backup using the ‘wp db export’ and ‘wp db import’ commands.
Q: Does reset-password include a method to display the new password in STDOUT? Often the email inbox is inaccessible causing the loss of the password in the first place.
A: No, but you can reset it directly using ‘wp user update admin –user_pass=password’
Q: I recently had a local development of a plugin that created a custom role on activation, e.g. “basic_member”. What I wanted to do is change that role to “starter_member”. I could easily search and replace that in my plugin files but I couldn’t do that with WP-CLI, which I thought would be easily enough to do. Is there a reason why you cannot do this?
A: Actually, you can – just use the ‘wp user update username –role=starter_member’ command replacing ‘username’ with your actual username.
Q: Can WP-CLI be used to clone a site?
A: Yes, you can do an export of your database and zip all your files and folders into an archive. Then, import your SQL file into a new database, restore files and you have a clone of that site!
Q: Is there a command to remove a site?
A: Your site consists of database and files. You can simply ‘wp db drop’ and then delete all the files and folders in that directory.
Q: This is all very new for me. As I’m learning, how easy would it be to break the site from WP-CLI?
A: It’s as easy as it is to break it from your admin panel. WP-CLI is powerful, but if you’re careful with your actions you shouldn’t break your site.
Q: How do I stop a media download while running a wp import command: (wp import wordpress.2019-07-09.xml –authors=create)
A: You can just terminate the command execution with Ctrl+C or Command+C.