Manage WordPress Through WP-CLI

Rommel Laranjo Blogs, DevOps, Software Development Tagged Tags: , , , , , ,
0

WordPress is a free opensource Content Management System (CMS) that is widely used in the internet today. Although there are many free opensource CMS to choose from, most users, businesses and developers chose WordPress because it is easy to use and customized. It also has a lot of plugins and themes to choose from in order to extend its capabilities.

Most users, newbies and experts alike, were used to configure and manage their WordPress sites through the browser. But there are some users who wished to configure and manage their sites through the Command Line Interface (CLI). This is the reason why the project WP-CLI was born.

WP-CLI is the command line interface of WordPress. You can use WP-CLI to install and configure WordPress, its plugins and more without using a web browser. Through WP-CLI, you can automate site management tasks by creating a script or scripts that will execute a series of WP-CLI commands.

If you wish to try out WP-CLI, you can follow the following tutorial which will guide you from installing WP-CLI up to installing WordPress plugin.

This tutorial was made under the following environment:

  • Ubuntu Linux 14.04
  • PHP 5.5.9
  • WordPress 4.8 (latest version as of this writing)

The minimum requirements in running WP-CLI are:

  • Unix-like environment (OS X, Linux, *BSD’s, Cygwin); limited support in Windows environment
  • PHP 5.3.29 or later
  • WordPress 3.7 or later

You have to verify first if your system has met these requirements before proceeding.

Installing WP-CLI

You download the Phar file first using curl or wget:

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

or

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

At this point, you can already use the execute the wp-cli.phar command from the directory where you downloaded it.

$ php wp-cli.phar --info

It will give you an output that is something like this:

PHP binary: /usr/bin/php5
PHP version: 5.5.9-1ubuntu4.21
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /tmp
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.2.1

But we want our wp-cli.phar to be accessible or to be executed anywhere in our system. We transfer it to the common user local binary path or /usr/local/bin. We also shorten the command into wp instead of typing wp-cli.phar:

$ sudo mv wp-cli.phar /usr/local/bin/wp
$ sudo chomd 555 /usr/local/bin/wp

Check again if you have successfully installed WP-CLI:

$ wp --info
PHP binary: /usr/bin/php5
PHP version: 5.5.9-1ubuntu4.21
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /tmp
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.2.1

Preparing Your Database

With WP-CLI, you can create a database based on wp-config.php using the wp db create command. But in this tutorial, I prefer to prepare and create the database using MySQL cli commands.

Database info:

  • name: wpcli_db
  • user: wpcli_admin
  • password: wpcli_admin_password

The following commands will create the MySQL database, user and grant privileges:

$ mysql -u root -e "create database if not exists wpcli_db;" -p
$ mysql -u root -e "create user 'wpcli_admin'@'%' identified by 'wpcli_admin_password';" -p
$ mysql -u root -e "grant all privileges on wpcli_db.* to 'wpcli_admin'@'%';" -p

Check if database was successfully created:

$ mysql -u wpcli_admin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 5.5.55-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wpcli_db           |
+--------------------+
2 rows in set (0.00 sec)

mysql> \q
Bye

Installing WordPress using WP-CLI

You already have installed WP-CLI and has successfully prepared the database for your site. This time, let us download and extract the latest WordPress version into your project directory (ex. wpcli_site) using the command wp core download:

$ cd /path/to/your/www
$ wp core download --path=wpcli_site
Creating directory '/path/to/your/www/wpcli_site/'.
Downloading WordPress 4.8 (en_US)...
md5 hash verified: b91248a7220a7fb1ca293c3a0ec8db6c
Success: WordPress downloaded.

WordPress core comes with a text file called wp-config-sample.php. This file contains a sample base configuration for WordPress. Usually, we rename this file into wp-config.php and edit the values then generate authentication unique keys and salts.

But let us use WP-CLI to generate the WordPress base configuration file wp-config.php. For security reasons, I tried to hide the database admin password from appearing in the command history by creating a temporary text file adminpass.txt which contains the DB password:

$ wp config create --dbname=wpcli_db --dbuser=wpcli_admin --dbprefix=wpcli_ --dbpass=`cat /path/to/your/adminpass.txt`
Success: Generated 'wp-config.php' file.

The above WP-CLI command will set DB_NAME to ‘wpcli_db‘, DB_USER to ‘wpcli_admin‘, DB_PASSWORD to ‘wpcli_admin_password‘ and $table_prefix to ‘wpcli_‘.

You can see the complete list of options available by running wp config create --help command.

Since we now have generated the base configuration file, it is time to install WordPress.

Note: You still need to configure your webserver (Apache / Nginx / etc) to serve your site.

Normally, WordPress is installed by accessing your site through the browser. This time, without lifting your fingers from the keyboard, let us install WordPress by using the WP-CLI command wp core install. Again for security reasons, I tried to hide the site admin password from appearing in the command history by creating a temporary text file siteadminpass.txt which contains the site admin password:

$ wp core install --url=wpclidemosite.v1 --title="WP-CLI DEMO SITE" --admin_user=pcsiteadmin --admin_password=`cat /path/to/your/siteadminpass.txt` --admin_email=rslaranjo@gmail.com
Success: WordPress installed successfully.

Alas! You may now check your newly installed WordPress site using your favorite browser.

WordPress WP-CLI Demo | Freelance Web Developer
WordPress WP-CLI Demo | Freelance Web Developer

Installing WordPress Plugin using WP-CLI

We now have a freshly installed WordPress site. Now, we want to install our favorite plugins into it. Before we proceed, let us first check what are the plugins currently installed in our site:

$ wp plugin list
+---------+----------+-----------+---------+
| name    | status   | update    | version |
+---------+----------+-----------+---------+
| akismet | inactive | available | 3.3.2   |
| hello   | inactive | none      | 1.6     |
+---------+----------+-----------+---------+

The command above, wp plugin list will get the list of plugins currently installed in your WordPress site. It also shows the status of the plugin, the version and whether there is an update available or not.

In this tutorial, let us install a powerful WordPress plugin called JetPack.

Just an overview about JetPack, as I mentioned earlier and will mention it again, it is a powerful WordPress plugin that monitor and collect valuable statistics of your site. It will automatically notify you through e-mail if your site goes offline or when it goes back up. These are just some of the many useful features of JetPack.

So now, let us try to search the JetPack plugin through WP-CLI:

$ wp plugin search jet
Success: Showing 10 of 37 plugins.
+----------------------------------------+--------------------------------------+--------+
| name                                   | slug                                 | rating |
+----------------------------------------+--------------------------------------+--------+
| Jetpack by WordPress.com               | jetpack                              | 82     |
| JetPack Contact Form Success Message   | jetpack-contact-form-success-message | 100    |
| Module Control for Jetpack             | jetpack-module-control               | 96     |
| TargetPay for Woocommerce              | targetpay-by-idealpluginsnl          | 0      |
| Jet Set Go                             | jet-set-go                           | 0      |
| AfterShip – WooCommerce Tracking       | aftership-woocommerce-tracking       | 98     |
| OSD Social Media Sharing               | osd-social-media-sharing             | 100    |
| WebReserv Booking Calendar 2.0         | webreserv-booking-calender-plugin    | 54     |
| Instajet                               | instajet                             | 0      |
| BoatDealer                             | boatdealer                           | 0      |
+----------------------------------------+--------------------------------------+--------+

Our search gave us 10 results. Let us install the plugin named Jetpack by WordPress.com. We use the slug value into our next command which is wp plugin install:

$ wp plugin install jetpack
Installing Jetpack by WordPress.com (5.2.1)
Downloading install package from https://downloads.wordpress.org/plugin/jetpack.5.2.1.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

There you have it. It says Success: Installed 1 of 1 plugins.

Let us again check all the plugins installed into our WordPress site.

$ wp plugin list
+---------+----------+-----------+---------+
| name    | status   | update    | version |
+---------+----------+-----------+---------+
| akismet | inactive | available | 3.3.2   |
| hello   | inactive | none      | 1.6     |
| jetpack | inactive | none      | 5.2.1   |
+---------+----------+-----------+---------+

There it is! JetPack appears in the list of plugins installed into our WordPress site. But the status of the JetPack plugin is still inactive.

You can easily activate a plugin in WP-CLI by using the command wp plugin activate:

$ wp plugin activate jetpack
Plugin 'jetpack' activated.
Success: Activated 1 of 1 plugins.

There it is again! It says Success: Activated 1 of 1 plugins. Let us check one more time the plugins installed in our WordPress site:

$ wp plugin list
+---------+----------+-----------+---------+
| name    | status   | update    | version |
+---------+----------+-----------+---------+
| akismet | inactive | available | 3.3.2   |
| hello   | inactive | none      | 1.6     |
| jetpack | active   | none      | 5.2.1   |
+---------+----------+-----------+---------+

You see the status of the JetPack plugin now is active.

One final command that we will use in this tutorial is wp plugin uninstall. Let me show you how to uninstall the sample plugin that comes with our freshly installed WordPress site, the Hello plugin.

$ wp plugin uninstall hello
Uninstalled and deleted 'hello' plugin.
Success: Uninstalled 1 of 1 plugins.

The Hello plugin is now successfully uninstalled and deleted. Let us list the plugins one more time to really confirm if it was uninstalled.

$ wp plugin list
+---------+----------+-----------+---------+
| name    | status   | update    | version |
+---------+----------+-----------+---------+
| akismet | inactive | available | 3.3.2   |
| jetpack | active   | none      | 5.2.1   |
+---------+----------+-----------+---------+

Now that you have seen a glimpse on how to manage WordPress through WP-CLI, it is for you to decide whether to continue exploring WP-CLI or to totally forget about it. But hopefully, some site admins or developers will find this tool useful especially if they love to automate things by writing scripts.

If you find this article useful or this helped you in anyway. Please share and comment below. I love to hear your thoughts about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *