The principle of WPCLI is to add install it on the server, add it to path so that it can be accessed from any folder and then enter the installation folder of the website for which you want to run commands.
The basic use of WP-CLI is: wp [COMMAND], e.g. wp config list will show you the content of wp-config.php file without any comments (lines that start with /* ):
wp config list
Plugins
Plugin setup is much simpler with WP-CLI, for example, to install and activate the Akismet plugin:
wp plugin install akismet --activate
To install multiple plugins at once:
wp plugin install advanced-custom-fields jetpack akismet --activate
To check if plugin is active or inactive run wp plugin status
wp plugin status
To update plugins wp plugin update name-of-the-plugin, for example:
wp plugin update akismet
or to update all plugins:
wp plugin update --all
Another handy command is skip-plugins, which allows you to temporarily disable plugins and load WordPress without them.
wp --skip-plugins=akismet
and to temporary disable all active plugins at once:
--wp plugin list --field=name --status=active --skip-plugins | xargs -n1 -I %
Themes
Show installed themes summary and various states:
wp theme list
To install and activate theme, copy it’s URL slug from the wp.org theme repository.
wp theme install twentytwentyone --activate
To create a child theme
wp child-theme FOLDER-NAME --parent_theme=PARENT-THEME-FOLDER --theme_name=CHILD-THEME-NAME
Here are a few more commands to activate, delete and update themes.
wp theme activate THEMENAME
wp theme update THEMENAME
wp theme delete THEMENAME
You can find more information in the official WP-CLI documentation.
Users
To list users:
wp user list
To create a new admin user, and auto-generate a password, use:
wp user create username [email protected] --role=administrator
Or to add a custom password use –user_pass=
wp user create username [email protected] --role=administrator --user_pass=3fg62g6732rg32
To change user password
wp user update USERNAME --user_pass=NEW_PASS
Delete a user and reassign all content from that user to the admin user:
wp user delete [email protected] --reassign=1
Database
wp db allows you to performs basic database operations using credentials stored in wp-config.php, for example to open a MySQL console using credentials from wp-config.php run:
wp db cli
To export database use:
wp db export
wp db optimize
wp db repair
I find wp db quite useful when you need to drop and import tables in database:
To drop all tables in a database run:
wp db reset
To import tables from .sql file:
wp db query < debug.sql
Common errors
Here are some of the most common WP-CLI errors that Ive personally encountered so far:
Error: This does not seem to be a WordPress installation.
Error: This does not seem to be a WordPress installation.
Enter the folder with your WP installation.
pwd – check in which directory you are currently, then to enter the right directory use cd /some/dir/wp/
bash: wp: command not found
wp is not added to your path, to workaround this, make sure to use the full path to the downloaded wp-cli.phar file
/home/USERNAME/public_html/wp-cli.phar
W3 Total Cache Error: some files appear to be missing or out of place
lf you are using W3TC plugin along with WP-CLI, you may notice the following error.
To fix this, before running any wpcli commands rename the wp-content/object-cache.php file
PHP Parse error: syntax error, unexpected ‘?’ in … /php/WP_CLI/Runner.php … eval()’d code on line 1 #
This is a well known bug on actual WP-CLI versions that is related to the Unicode BOM inside the wp-config.php file. To fix it, simply open the wp-config.php file and remove the code from the beginning: