Install WP themes and plugins via WP-CLI

How to use the WP-CLI to install and activate themes and plugins in wordpress

The WP-CLI or the Wordpress Command Line Interface is a superb tool to manage your wordpress blog and perform several tasks like installing, uninstalling and updating plugins, managing the wordpress database and so on. You can even remotely create posts and assign categories without having to login to the wordpress admin dashboard. However, to do it, first you need to have the CLI installed on your server. Below, we have outlined the method to download and install the WP-CLI. The installation takes only a few minutes and then you are an official WP-CLI user. Now, you can use the cli to activate new themes and plugins and to do many more things.

How to Install WP-CLI

The official method to install WP-CLI :

Download wp-cli.phar using wget or curl

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Verify it works:

$ php wp-cli.phar --info

The output must look like the following:

OS: Linux 5.15.0-1028-aws #32-Ubuntu SMP Mon Jan 9 12:28:07 UTC 2023 x86_64

Shell:  /bin/bash

PHP binary: /usr/bin/php8.1

PHP version: 8.1.2-1ubuntu2.11

php.ini used:   /etc/php/8.1/cli/php.ini

MySQL binary:   /usr/bin/mysql

MySQL version:  mysql  Ver 15.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

SQL modes:

WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli

WP-CLI vendor dir:  phar://wp-cli.phar/vendor

WP_CLI phar path:   /home/ubuntu

WP-CLI packages dir:

WP-CLI cache dir:   /home/ubuntu/.wp-cli/cache

WP-CLI global config:

WP-CLI project config:

WP-CLI version: 2.7.1

Now, make the file executable and move it somewhere in your PATH so you can run commands by just typing wp.

$ chmod +x wp-cli.phar$ sudo mv wp-cli.phar /usr/local/bin/wp

Now, if you type wp –info, you will receive the same output as you did when you ran:

 $php wp-cli.phar –info

This will show that you have successfully installed the wp-cli and you can start using it to manage your wordpress installation.

Install and Uninstall themes and plugins in wordpress using WP-CLI

When you install wordpress, it has the default twenty twenty three theme enabled. Now, you can change the theme and add more plugins as per your need.

Let’s add the generatepress theme and install Yoast for seo as well as wp-fastest-cache for caching.

To install the generatepress theme and activate it on your website, run the following command:

$wp theme install generatepress --activate --path=/var/www/html

In the above command, you need to add the path to your wordpress installation at the end like –path=/path to/root/folder or –path=/var/www/html. (otherwise you can run these commands from the root folder and you will not need to add the path at the end of each command. Use change directory ‘cd’ command to go to root folder like $ cd /var/www/html )

You will see output like the following:

Installing GeneratePress (3.2.4)

Downloading installation package from .

Using cached file ‘/home/ubuntu/.wp-cli/cache/theme/generatepress-3.2.4.zip’…Unpacking the package…Installing the theme…

Theme installed successfully.

Activating ‘generatepress’…Success: Switched to ‘GeneratePress’ theme.Success: Installed 1 of 1 themes.

You can also update the theme using the theme update command. For example, if I try to update the generatepress theme I installed just now:

$ wp theme update generatepress --path=/var/www/html

Success: Theme already updated

Since, I have just installed the latest version, the output is theme already updated.

There are four themes in my wordpress installation including the just installed Generatepress. The system installs the default themes with wordpress which include twenty twenty one, twenty two and twenty three. I can keep twenty twenty three and delete the rest since it is no use to keep useless themes. 

Check out the themes currently installed using the following command:

$ wp theme list –path=/var/www/aim-blog/html
+——————-+———-+——–+———+
| name              | status   | update | version |
+——————-+———-+——–+———+
| generatepress     | active   | none   | 3.2.4   |
| twentytwentyone   | inactive | none   | 1.7     |
| twentytwentythree | inactive | none   | 1.0     |
| twentytwentytwo   | inactive | none   | 1.3     |
+——————-+———-+——–+———+

Delete the unnecessary themes:

$ wp theme delete twentytwentyone --path=/var/www/html

Deleted ‘twentytwentyone’ theme.
Success: Deleted 1 of 1 themes.

You can also delete all the themes at once except the active theme:

$ wp theme delete --all --path=/var/www/html

Now, we need to install plugins. First, lets get a list of the plugins installed on our blog.

$ wp plugin list --path=/var/www/html

+——————+———-+———–+———+

| name         | status   | update | version |

+——————+———-+———–+———+

| akismet      | inactive | available | 5.0.1   |

| hello        | inactive | none  | 1.7.2   |

| wp-fastest-cache | active   | none  | 1.1.2   |

+——————+———-+———–+———+

There are three plugins currently installed on the blog including wp-fastest-cache and the two plugins that come with default installation of wordpress. I can delete the two plugins including Akismet and Hello Dolly and add new ones.

$ wp plugin delete akismet hello --path=/var/www/html

Deleted ‘akismet’ plugin.

Deleted ‘hello’ plugin.

Success: Deleted 2 of 2 plugins.

Another way of deleting the two plugins was to exclude wp-fastest-cache and delete the rest.

$ wp plugin delete –all –exclude=wp-fastest-cache -–path=/var/www/html

The above code leaves the plugins listed after –exclude= and deletes the rest. You can add more plugins separated by commas like –exclude=wordpress-seo,wp-fastest-cache

I also need to install the yoast seo plugin. However, I will need the slug to install the yoast seo plugin. So, I will first try to find out the slug by searching for yoast in the wordpress repository.

$ wp plugin search yoast --path=/var/www/html

Success: Showing 10 of 928 plugins.

+———————————————+————————————+——–+

| name                                  | slug                           | rating |

+—————————-+————————————+——–+

| Yoast SEO              | wordpress-seo                  | 96 |

| Yoast Duplicate Post     | duplicate-post                 | 94 |

| WP Fastest Cache   | wp-fastest-cache                   | 98 |

| Yoast Test Helper      | yoast-test-helper              | 98 |

| LiteSpeed Cache       | litespeed-cache                | 96 |

| ACF Content Analysis for Yoast SEO | acf-content-analysis-for-yoast-seo | 84 |

| All in One SEO – Best WordPress SEO Plugin | all-in-one-seo-pack    | 94 |

| fic                                             |                                |    |

| Rank Math SEO           | seo-by-rank-math               | 98 |

| Translate WordPress with GTranslate   | gtranslate                     | 98 |

| Import Settings into WordPress SEO by Yoast | yoast-seo-settings-xml-csv-import  | 78 |

+—————————————————+————————————+——–+

According to the above output, the slug for yoast seo is wordpress-seo.

So, now we can install yoast:

$ wp plugin install wordpress-seo --activate --path=/var/www/html

Installing Yoast SEO (20.3)

Downloading installation package from .

Unpacking the package…

Installing the plugin…

Plugin installed successfully.

Activating ‘wordpress-seo’…

Plugin ‘wordpress-seo’ activated.

Success: Installed 1 of 1 plugins.

The output shows, we have successfully installed Yoast SEO on our blog.

Now, there are two plugins active on the blog. All I need to do is to configure them from the admin dashboard.

If you are running your blog on nginx webserver, install the Nginx helper plugin for purging cache:

$ wp plugin install nginx-helper --activate --path=/var/www/html

Installing Nginx Helper (2.2.2)

Downloading installation package from .

Unpacking the package…

Installing the plugin…

Plugin installed successfully.

Activating ‘nginx-helper’…

Plugin ‘nginx-helper’ activated.

Success: Installed 1 of 1 plugins

To deactivate the same plugin, run the following command:

$ wp plugin deactivate nginx-helper –path=/var/www/html

Plugin ‘nginx-helper’ deactivated.
Success: Deactivated 1 of 1 plugins.

If you want to deactivate and uninstall a plugin:

$wp plugin deactivate plugin --uninstall --path=/path/to/root/folder  

Sometimes, you may need to deactivate all the installed plugins to discover a particular issue or plugin conflict. To do that run the following command:

$ wp plugin deactivate --all --path=/var/www/html

So, this is how you can easily install, uninstall, activate and deactivate plugins using the WP-CLI.

Thanks for reading!