How can I use the WP-CLI in my WordPress Workflow?
If you have been in the WordPress community for any length of time, you surely have heard of the WP-CLI. If you have not heard of the WP-CLI, it is essentially a set of command line tools that allow you to interact with your WordPress site quickly and efficiently from terminal instead of using the WordPress admin dashboard. Without practical examples of when to use the WP-CLI, you may be thinking that the CLI is just a good idea in theory, but you wouldn’t lean towards using it right away in your next project. I’m going to share with you some of the essential WP-CLI commands that will save you time and energy.
Installing the WP-CLI
To install the WP-CLI, you will need to first SSH into your server. Once you are SSH’d in, run:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
This will pull the latest release of the WP-CLI into current directory. Next, you will need to give the WP-CLI the correct permissions. Do this by running:
chmod +x wp-cli.phar
Note: If you using VVV for a local WordPress environment, you do not need to install the WP-CLI. Instead, you would
vagrant ssh then you will have the command line tools available to you.
Basic WP-CLI commands
Here are some basic commands that will let you ease your way into using the WP-CLI in your workflow:
Check which version of WordPress you have by running:
wp core version
Find out which themes are installed and active by running:
wp theme list
Or alternatively check which plugins are installed and active or are needing updates by running:
wp plugin list
Only want to know what your inactive plugins are?
wp plugin list --status=inactive
That is great to know what plugins you have that are inactive, but what if you want to delete inactive plugins?
wp plugin delete
Let’s update all of our plugins:
wp plugin update --all
Let’s try generating 5 posts:
wp post generate --count=5
You can also generate 3 custom post types:
wp post generate --post_type=movies --count=3
A little bit more advanced
Want to create a new WordPress project with a specific url, title, and create a unique admin user from the command line?
wp core install --url="your_domain" --title="Blog Title" --admin_user="admin username" --admin_password="enter_your_password" --admin_email="enter_your_email"
Do a search and replace throughout your entire site:
wp search-replace 'http://example.dev' 'http://example.com' --all-tables
How about doing a search and replace and then exporting that into a new sql file, that way we don’t change our current database:
wp search-replace 'http://example.dev' 'http://example.com' --all-tables --export=newsqlfile.sql
Tired of having to go in to the WordPress Admin dashboard to save permalinks? Just run this:
wp rewrite flush
But wait, there’s more
The WP-CLI is a very robust tool that can help your workflow tremendously! There are unlimited options as to what you can do. This was just a quick overview of some of the commands, for a more thorough list of the commands you can run with the WP-CLI, go to the WordPress CLI Commands Doc.