WordPress
Article

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

By Ahsan Parwez

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).

  • http://SalaryNet30.com Tresa Freeman

    like Phillip answered I can not believe that you can make $9890 in four weeks on thelaptop.try this website on !`my` `prof1le`!

    ###Wwwwwwwwwwwwww

  • Fahad (Eddie)

    Thanks and Highly Appreciable Ahsan

    • http://infopay50.com Elsie Ventura

      ““I “am “ finally getting 95 Dollars“` an hr,….It’s time to take some action` and you can join it too.It is easy way to get rich`.Three weeks from now you will wish you have started today….

      +++++++++++++>>> Vis!t My Pr0f1le:)“`
      q2w

    • http://DestinyRescue.cf Virgie Calvo

      Is this how it’s possible to earn ninety-seven bucks /hr…? After being un-employed for 6 months, I started freelancing over this web-site and now I cannot be happier. After 4 months on my new job my income is round about dollars 7500/per month working 20hrs a week ..

      For web address open my ṖRὀÍLÊ
      ϣy

  • hk0i

    Mine can’t seem to connect to the database. It spits out the default WP html for being unable to connect.
    Do you have to do any configuring to get it to work?

    • http://www.cloudways.com/en/ Ahsan Parwez

      Hi,

      Is the error coming while taking backup of the DB?

      • hk0i

        Any time I typed anything with it. Including no params and the help option.
        Turns out it was my wp-config and the way I was determining the db credentials wasn’t working from the command line invocation of the script.

        Edit:
        Or actually that plus the fact I wasn’t running it on the same machine as the server. I just wanted to update my wp install from command line but it wouldn’t work.

        • http://www.cloudways.com/en/ Ahsan Parwez

          I have verified again, everything is working fine.

          You should install wp-cli on same server with root access. And to run the WP-CLI commands it is a good practice to create a separate user without root access, else you will get a warning message and have to include –allow-root on each command you run

          • hk0i

            I think in my case the issue was that WP was running in docker and my wp-config.php has some custom multiple environment support baked in

          • http://www.cloudways.com/en/ Ahsan Parwez

            Might be, I haven’t used docker for this tutorial, have used DigitalOcean.

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Instant Website Review

Use Woorank to analyze and optimize your website to improve your website to improve your ranking!

Run a review to see how your site can improve across 70+ metrics!

Get the latest in WordPress, once a week, for free.