Published on January 21, 2020 | Posted in Wordpress
My most used WP-CLI commands
I’ve only just recently started WP-CLI(WordPress Command Line Interface). It’s something I wish I started using earlier. Basically what WP-CLI does is allows you to run typical WordPress actions such as updating user passwords, removing plugins and backing up a WordPress database from the command line in the root of your WordPress installation.
Checking if WP-CLI is currently installed
To use WP-CLI you going to need command line access to your website environment. As always when doing actions like this, it’s important to make a backup of your site before you make a start.
We need to check if you currently have WP-CLI installed to do so run the following command. From the directory of your site in the command line.
wp --info
If installed something similar to the below should be returned.
OS: Linux 4.13.0-21-generic #24-Ubuntu SMP Mon Dec 18 17:29:16 UTC 2017 x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php7.2
PHP version: 7.2.2-3+ubuntu17.10.1+deb.sury.org+1
php.ini used: /etc/php/7.2/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
If it is not installed we will install it now. If it is installed skip the following step below.
Installing WP-CLI
Before we start it important to mention that you going to need Sudo privileges. The first thing we to do is fetch the WP-CLI script.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Now we make the file an executable and move it to the system path.
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Now to check if it’s been installed by running the command from earlier. You should now see a similar message to the one below.
OS: Linux 4.13.0-21-generic #24-Ubuntu SMP Mon Dec 18 17:29:16 UTC 2017 x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php7.2
PHP version: 7.2.2-3+ubuntu17.10.1+deb.sury.org+1
php.ini used: /etc/php/7.2/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
wp user create
What this command does is allows you to create a new user from the command line. I found this useful as I recently had to create a new site and had to add in several new users. The command gives you all the options you would find when creating a new user as you would when doing so on your WordPress site such as user role, display name and sending a confirmation email.
wp user create [email protected] user-login=test-user --send-email --first_name=test --last_name=user --user_pass=3y*nFjZTSuOD --role=administrator
More information – https://developer.wordpress.org/cli/commands/user/create/
wp plugin install
This command does what it says on the tin it allows you to install a WordPress plugin and then activate it. You can install the plugin from the WordPress repository, url or even a zip file.
wp plugin install all-in-one-wp-migration --activate
wp db export and wp db import
These are two separate commands but I’m grouping them together. What it does is allows you to export your site database into a mysql dump. The import command then allows you to import the mysql dump to another server. I’ve recently been moving my sites over to a new server and I found this command really useful.
wp db export database.sql
wp db import database.sql
More information – https://developer.wordpress.org/cli/commands/db/import/, https://developer.wordpress.org/cli/commands/db/export/
wp search-replace
This command does a search and replace on your entire database on a string of text. It has a dry-run flag which you can run if you want to do a test before you run the full command.
wp search-replace duaneblake duane-blake
More information – https://developer.wordpress.org/cli/commands/search-replace/
The above commands are most used command which I use. There are a load more which you may find useful I encourage you to review the WP-CLI site for a list of all commands.