How To Manage WordPress Website Using WP-CLI

If you are a WordPress developer, then definitely you heard about WP-CLI. WP-CLI is a command line interface for WordPress. Using this tool you can manage a WordPress website without login to your dashboard. Once installed, WP-CLI provides you some commands through which you can install/update WordPress, install/update/activate/deactivate plugins, backup database and much more.

For using WP-CLI, you should have SSH access from your hosting. We are using Bluehost to host our website and they provides SSH access for customers.

As a WordPress developer, you should use this tool for your projects. Its an official command line interface for WordPress. It also faster your development process and saves you a lot of time.

WP-CLI Installation

You can install WP-CLI in different ways. If someone is good with SSH commands then they can follow installation process using those commands. You can get details about this here.

But we recommend installing WP-CLI is through Composer. Installation process through Composer is easy and anyone can do it. Make sure you have installed Composer on your system and then run the command below.

composer global require wp-cli/wp-cli

Above command would install WP-CLI globally on your system. That means, if you are on your local server then you are now able to use this tool on all of your WordPress websites. And if you are on shared hosting server then also you will use this interface for all of your WordPress projects.

If you don’t want to install it globally. Then create a composer.json file in your project root directory and add the below lines in it.

composer.json

{
    "require" : {
        "wp-cli/wp-cli" : "~0.22",
        "psy/psysh" : "~0.6"
    }
}

After adding the above lines, open the terminal in your project root directory and run the command:

composer install

.

WordPress Download Using WP-CLI

To create a WordPress website, we need to first download it. For this, we go to the official website of WordPress and download it. Through WP-CLI, you don’t need to visit any website. Below command will download extracted version of WordPress for you.

wp core download

This command downloads the latest version of WordPress.

If you want to update your WordPress version then run the below command.

wp core update

Installing WordPress Plugins With WP-CLI

With WP-CLI, we can install single or multiple plugins in the single command. For plugin installation, we need to pass the slug of a plugin. For example, if we need to install Contact Form 7 plugin then it’s slug is contact-form-7.

Contact Form 7 Slug

Now, to install Contact Form 7 plugin you will run the command:

wp plugin install contact-form-7

Same way, we can install multiple plugins with the command:

wp plugin install contact-form-7 w3-total-cache wordpress-seo

These commands will install a plugin on your website. But we can go one step ahead. That means, using the WP-CLI we can install and activate the plugin in a single command:

wp plugin install wp-optimize --activate

Command for deactivating plugin:

wp plugin deactivate wp-optimize

If you need to update a single plugin then command is:

wp plugin update wp-optimize

And if you need to update all plugins then the command is:

wp plugin update --all

WP-CLI Commands For Media

While working on WordPress projects, sometimes we need to register extra image sizes. And after adding custom image sizes, we need to regenerate all existing thumbnails. We can regenerate thumbnails with WP-CLI using the command:

wp media regenerate --yes

We can even set featured images with WP-CLI for any post or page.

Let’s say you have to set a featured image for the post which has id 100. In the command below we have set the path of the image, post id, and image title. For the image path, we follow the Windows OS system path. In your case, this path will be different.

wp media import C:\Users\sajid\Downloads\image.png --post_id=100 --title="A downloaded picture" --featured_image

Export Database Using WP-CLI

When we are finished with our development process, we need to move WordPress website to other domain. To do so, we need to change URLs from the WordPress database to match with our new domain. With WP-CLI we can search replace URLs in the database and export it by the command:

wp search-replace http://example1.com http://example2.com --export=C:\Users\sajid\Downloads\database.sql

Here we are replacing http://example1.com URL with http://example2.com URL.

There are some basic commands we discussed. WP-CLI provides a large set of commands for WordPress development. Please visit the page WP-CLI Commands for getting list of all available commands.

We hope you understand how to manage a WordPress website with WP-CLI. We are eager to know your thoughts in the comment section below.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published.