1. Home
  2. Applications Management
  3. WordPress
  4. Useful WP-CLI Commands with Examples

Useful WP-CLI Commands with Examples

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.

If you are a ChemiCloud customer on our WordPress hosting plans you have access to WP-CLI already, simply SSH into your server to get started.

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.

wp plugin

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

e.g: https://wordpress.org/plugins/really-simple-ssl/

  • 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

wp option

  • Get site URL
# Get site URL.
wp option get siteurl

# Set site URL
wp option set siteurl

wp search-replace

This is a useful command that searches/replaces strings in the database. e.g.: replacing and 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
ParameterDescription
--preciseForce the use of PHP (instead of SQL) which is more thorough, but slower.
--recurse-objectsEnable recursing into objects to replace strings.
--all-tables-with-prefixEnable replacement on any tables that match the table prefix even if not registered on $wpdb.

If you want to run a test query without actually making changes you can add –dry-run parameter.

wp user

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

wp core

  • Check for missing or modified core files
If you want to check if there are missing or modified core WordPress files you can use:
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

wp cache

  • Flush cache
WP-CLI  has the option to flush the default cache. You can rely on this option in case your website cache is not handled with the help of a caching plugin. To flush the cache, use the following command in your terminal:
# Flush cache
wp cache flush

Conclusion

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!

Don’t forget that ChemiCloud’s infrastructure supports WP-CLI out of the box. If you’d like to experience top speed and reliability and have access to great tools like WP-CLI, try our WordPress hosting and have your website migrate today free of charge and with no downtime.

Updated on July 24, 2019

Was this article helpful?

Related Articles

Need help?
We're always here for you. Submit a ticket or Chat with a live person.
Submit Ticket