How to Install and Use WP-CLI to Manage WordPress Websites

Share this article

Speeding up your work process should be one of your top priorities. Simply put, if you do more work in less time, then you will have more time to work on more projects, study and rest. WP-CLI is one of the command line tools specifically made to manage your WordPress websites through the command line. With a few simple commands, you can manage WordPress without even needing to login to your WordPress admin and navigate through the pages. WordPress WP-CLI In this tutorial, we’re going to learn exactly what WP-CLI is, how to install it and an overview of the basic commands of this tool. WP-CLI will be particularly useful if you are a WordPress developer, System Administrator or run a business built around WordPress. This command line tool will greatly help you do more in less time. For example, taking backups, updating WordPress and plugins, publishing content and querying databases can be accomplished relatively quickly.

Requirements of WP-CLI

To install WP-CLI, make sure you have a hosting account with SSH access. Most shared hosting providers do not give you access to SSH. Providers like DigitalOcean, Linode and AWS are ideal for this if you love to spin up your own servers. Other requirements are basic:
  1. PHP 5.3.2 or later.
  2. WordPress 3.4 or later.
  3. UNIX like environment like Linux.

How to Install WP-CLI

Step 1

First connect to your server’s command line through SSH. You can install the latest version of WP-CLI using the cURL or wget command. The WP-CLI installation file is available as a phar file and the latest version can be found at: https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar Using the cURL command, type:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar This will download and extract the WP-CLI files to your root user directory.

Step 2

Next, we will set the permissions to make it executable. Enter the following command:
chmod +x wp-cli.phar

Step 3

Optionally, we can move wp-cli.phar to a folder and rename it to wp. This will help us use the WP-CLI commands by just typing ‘wp’ at the start of the commands.
 sudo mv wp-cli.phar /usr/local/bin/wp
That’s it! Now we can jump in to use many of the WP-CLI commands.

Basic WP-CLI Commands

To keep it simple, we’re going to overview the basic WP-CLI commands. You can explore more commands here.

Using the WP-CLI Help System

WP-CLI comes with a full help system that you can access by typing “wp help”, for example:
wp help cache
Will display information as:

NAME

wp cache                                                                                                                                   

DESCRIPTION

Manage the object cache.                                                                                                                   

SYNOPSIS

wp cache <command>                                                                                                                         

SUBCOMMANDS

add – Add a value to the object cache. decr – Decrement a value in the object cache. delete – Remove a value from the object cache. flush – Flush the object cache. get – Get a value from the object cache. incr – Increment a value in the object cache. replace – Replace an existing value in the object cache. set – Set a value to the object cache. type – Attempts to determine which object cache is being used.

EXAMPLES

wp cache set my_key my_value my_group 300                                                                                                

wp cache get my_key my_group                                                                                                             
You can use the help system to understand what you can do with the commands and their respective subcommands.

Installing WordPress with WP-CLI

If you haven’t installed WordPress by using any other method on your server, then you can easily do so using WP-CLI command wp core install. Along with that command we need to pass parameters such as URL, Title, Admin Username, Password and Admin Email.
wp core install --url="your_domain"  --title="Blog Title" --admin_user="admin username" --admin_password="enter_your_password" --admin_email="enter_your_email"

Installing Themes with WP-CLI

Importing and installing themes is much quicker through WP-CLI than going into WordPress admin, searching and then activating it. It connects your server directly to the WordPress theme repository
and imports the theme in a matter of a few seconds. For example, to install a theme like TwentyTen, we will use the following command:
wp theme install twentyten
Similarly, to install P2 theme, our command will become:
wp theme install p2
To activate the themes on your WordPress website, you will need to use the following command:
wp theme activate p2

Installing Plugins with WP-CLI

Just like the themes, plugins can also be installed right from the official repository. Installation is seamless and takes almost no time. For example: wp plugin install woocommerce The above command will install the WooCommerce plugin on your website. To activate it, we will use the command:
wp plugin activate woocommerce
Similarly, to deactivate any plugin, the command becomes:
wp plugin deactivate woocommerce

Updating WordPress Core, Themes and Plugins with WP-CLI

Repetitive and often time consuming task of updating the WordPress core, themes and plugins can be easily carried out through the command line. To update the WordPress core to the latest stable release of WordPress, the command is:
wp core update
If you want to update to a specific version, for example you have WordPress 3.7 and want to move to WordPress 4.0 instead of WordPress 4.3, use the command:
wp core update --version=4.0
Or, if for any reason you want to revert back your WordPress site to the previous version, the command is:
wp core update --version=3.9 --force
To update plugins, you can either define one plugin to update or better still, update all the plugins in one go.
wp plugin update woocommerce

wp plugin update --all
A similar method applies to the themes. You can update WordPress themes through WP-CLI with following command
wp theme update twentyten
wp theme update --all

Conclusion

WP-CLI is indeed a powerful tool that can be used to manage your WordPress sites through the command line. There are also many more commands to manage your database, take backups, manage posts, comments and WordPress multisite It definitely speeds up your tasks, and makes it really easy to manage the sites. Luckily, there are hosting providers that provide WP-CLI by default on their WordPress installations (you can find a maintained list here).

Frequently Asked Questions (FAQs) about WP-CLI

What is the Importance of WP-CLI for WordPress Management?

WP-CLI is a powerful tool that allows you to manage your WordPress website from the command line. It provides a faster and more efficient way to perform tasks that would otherwise require navigating through the WordPress admin interface. With WP-CLI, you can install and update plugins, create and manage users, posts, and comments, and even perform database operations. It’s particularly useful for developers and administrators who manage multiple WordPress sites, as it allows them to automate repetitive tasks.

How Do I Install WP-CLI on Windows?

Installing WP-CLI on Windows involves a few more steps compared to Linux or Mac. You’ll need to install a Unix-like environment such as Cygwin or Git Bash. Once you have that set up, you can download the WP-CLI Phar file, make it executable, and move it to a directory in your PATH. Remember to regularly update WP-CLI to ensure you have the latest features and security updates.

Can I Use WP-CLI to Manage WordPress Multisite?

Yes, WP-CLI fully supports WordPress Multisite. You can perform tasks on the network level or on individual sites within the network. For example, you can install and activate plugins across all sites in the network with a single command, or create a new site within the network.

How Do I Update WP-CLI?

Updating WP-CLI is straightforward. You can use the command ‘wp cli update’ to update to the latest stable version. If you want to use a nightly build or a specific version, you can use the ‘–nightly’ or ‘–version=‘ options respectively.

What Are Some Common WP-CLI Commands?

WP-CLI includes a wide range of commands for managing your WordPress site. Some common ones include ‘wp plugin install’ to install a plugin, ‘wp user create’ to create a new user, and ‘wp post create’ to create a new post. You can use ‘wp help’ to get a list of all available commands and ‘wp help ‘ to get detailed information about a specific command.

How Can I Automate Tasks with WP-CLI?

WP-CLI can be used in conjunction with shell scripts to automate repetitive tasks. For example, you could write a script that updates all your plugins, creates a backup of your site, and then sends you an email when it’s done. The possibilities are virtually endless.

Can I Extend WP-CLI with Custom Commands?

Yes, WP-CLI is highly extensible and allows you to write custom commands. This is done by creating a PHP class that defines the command and its parameters, and then registering it with WP-CLI. This allows you to tailor WP-CLI to your specific needs and workflows.

How Do I Troubleshoot WP-CLI Errors?

WP-CLI provides detailed error messages that can help you troubleshoot problems. If you’re having trouble with a specific command, try running it with the ‘–debug’ option to get more information. You can also check the WP-CLI handbook and community forums for solutions to common issues.

Is WP-CLI Secure?

WP-CLI is as secure as the environment it’s run in. It doesn’t introduce any additional security risks to your WordPress site. However, because it provides powerful functionality, it’s important to ensure that only trusted users have access to it.

Where Can I Learn More About WP-CLI?

The official WP-CLI website and the WP-CLI handbook are great resources for learning more about WP-CLI. They provide detailed documentation on all aspects of WP-CLI, including installation, usage, commands, and development. You can also find tutorials and articles on various WordPress and developer blogs.

Ahsan ParwezAhsan Parwez
View Author

Ahsan is an advocate of Open Source platforms specially WordPress. He is currently working at Cloudways as the WordPress Community Manager. In his free time he loves to discover new things in WordPress and loves to teach WordPress.

ChrisBcliCommand lineWordPresswp-cli
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week