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

    Ahsan Parwez
    Share

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