What is WP-CLI?
WP-CLI is a set of command-line tools for managing WordPress installations, without using a web browser You can update plugins, configure multisite installs and much more.
Useful WP-CLI Commands with Examples
Commands always start with wp followed by a command and subcommand, followed by required and optional parameters.
Note that WP-CLI will work with the WordPress installation you are currently in the terminal. If you switch directories to go to another WordPress installation, it will work with that one.
Now we’ll take a look at some useful WP-CLI commands with examples.
Manages plugins, including installs, activations, and updates.
- Installing multiple plugins
To install multiple plugins here’s an example that downloads and activates 3 plugins:
# Install multiple plugins wp plugin install really-simple-ssl jetpack ninja-forms --activate
- Updating a plugin:
To update a plugin here’s an example that downloads the new version and updates a plugin:
# Update plugin wp plugin update "plugin-name"
- Activate, deactivate and delete a plugin:
# Activate plugin $ wp plugin activate hello Plugin 'hello' activated. Success: Activated 1 of 1 plugins. # Deactivate plugin $ wp plugin deactivate hello Plugin 'hello' deactivated. Success: Deactivated 1 of 1 plugins. # Delete plugin $ wp plugin delete hello Deleted 'hello' plugin. Success: Deleted 1 of 1 plugins.
- Get a list of plugins:
wp plugin list
For example if you want to see only a list of all active plugins which have updates available, you can use:
wp plugin list --field=name --status=active --update=available
- Get site URL
# Get site URL. wp option get siteurl # Set site URL wp option set siteurl
This is a useful command that searches/replaces strings in the database. e.g.: replacing an old domain name with a new domain name.
Before running this command we strongly recommend you to do a backup for your database, using the following command line:
wp db export
Once you’ve ensured that you have a backup for the database you can safely use the wp search-replace command:
wp search-replace 'http://old-domain.com/' 'http://new-domain.com/' --precise --recurse-objects --all-tables-with-prefix
|--precise||Force the use of PHP (instead of SQL) which is more thorough, but slower.|
|--recurse-objects||Enable recursing into objects to replace strings.|
|--all-tables-with-prefix||Enable replacement on any tables that match the table prefix even if not registered on $wpdb.|
Manages users, along with their roles, capabilities, and meta.
- List users
# List users wp user list # List users with administrator role wp user list --role=administrator
- Create a new user
# Create a new administrator user. wp user create john [email protected] --role=administrator Success: Created user 5. Password: C2p0MS4zCa # Create a new author user. wp user create john [email protected] --role=author Success: Created user 6. Password: HoW5tGXIKf
- Delete user and re-assign posts to other user
# Delete user john and reassign posts to user paul $ wp user delete john --reassign=paul
- Reset the password for one user
Step 1: $ wp user list to see all users. Find the ID of the user you'd like to update. Step 2: update the user $ wp user update 1 --user_pass=2DeBa10XjxD3Z8Ma replacing "1" with the id of the user you want to update.
- Change admin email address for one user
Step 1: $ wp user list to see all users. Find the ID of the user you'd like to update. Step 2: update the user $ wp user update 1 --user_email=<email> replacing "1" with the id of the user you want to update and <email> with the new email address.
- Set or modify a user role
Step 1: $ wp user list to see all users. Find the ID of the user you'd like to update. Step 2: $ wp user set-role 1 administrator Success: Added johndoe (1) to http://example.com as author. # Available roles: administrator editor author contributor subscriber
- Check for missing or modified core files
wp core verify-checksums
- Reinstall WordPress Core
You can reinstall WordPress core using WP-CLI. The following command would download WordPress core without the default themes and plugins.
wp core download --skip-content --force
- Flush cache
# Flush cache wp cache flush
WP-CLI really is the developer and website manager’s dream come true. Site owners can handle updates and other tasks for multiple sites with single commands.
If you haven’t already tried WP-CLI we encourage you to give it a go!