WP-CLI for working with WordPress

by , , revisited on

We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

WP-CLI is an excellent command-line utility for WordPress websites.

It’s like the secret weapon of WordPress developers and site managers. With WP-CLI, you can do a lot of things without ever having to log in to the admin area of your site.

For example, you can install and update WordPress, themes, and plugins all from the command line. This is especially handy if you’re managing multiple sites or working on a development server.

Install WP-CLI

Install WP-CLI on CentOS/RHEL servers

sudo yum -y install https://extras.getpagespeed.com/release-latest.rpm
sudo yum -y install wp-cli wp-cli-completion-bash

Other distributions

On other distros, e.g. Debian, you can install WP-CLI using these commands:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Let’s review the basic usage cases for the utility. You should run the commands by connecting as your website SSH user.

Install WordPress

First, change the working directory to the directory where you want to install the new WordPress site.
We assume you’ve already created MySQL database and user already.
In case you haven’t done that so far, you can do so in PhpMyAdmin or your favorite SQL client.
But we prefer the command line. It’s easy with:

mysqladmin create site1
mysql -e "CREATE USER 'site1'@'localhost' IDENTIFIED BY 'password';"
mysql -e "GRANT ALL PRIVILEGES ON site1.* TO 'site1'@'localhost';"

Now we have created site1 database and site1 database user, and gave the necessary privileges for access.

Next, these commands will download and install latest WordPress, all using WP-CLI.

Install standard WordPress

wp core download --locale=en_US --force
wp core config --dbname=site1 --dbuser=site1 --dbpass=password --locale=en_US --dbcollate=utf8_unicode_ci
wp core install --url=http://www.example.com/ --title=Wordpress --admin_user=username --admin_password=secret --admin_email=my@gmail.com

Install WordPress with specific language

Let’s suppose that we want WordPress with Russian localization:

wp core download --locale=ru_RU --force
wp core config --dbname=site1 --dbuser=site1 --dbpass=password --locale=ru_RU --dbcollate=utf8_unicode_ci
wp core install --url=http://www.example.com/ --title=Wordpress --admin_user=username --admin_password=secret --admin_email=my@gmail.com

Create WordPress administrator using command line

wp user create username email@example.com --role=administrator --user_pass=secret

Change WordPress site URL

It is much easier and bulletproof to use command line interface for changing WordPress site URL.

Let’s change the address from http://example.com to https://example.com. Simply run:

wp search-replace 'http://example.com' 'https://example.com' --skip-columns=guid

If you’ve decided that you want to follow the standards and change the primary WordPress domain from example.com to www.example.com, run:

wp search-replace 'http://example.com' 'http://www.example.com' --skip-columns=guid

Read more about changing WordPress site URL.

Manage WordPress plugins

It is one of the most useful features. It might come useful for your automation scripts. Suppose that we clone our WordPress website every night and put it on a staging domain for testing.

We want the staging WordPress site to be available at staging.example.com. We can of course use search-replace functionality. But for the staging site, we can install any-hostname plugin. We also want to disable any kind of caching on the staging website:

wp plugin install any-hostname --activate
wp plugin deactivate autoptimize duo-wordpress w3-total-cache wp-opcache-patch

In the above commands, we install and activate Any Hostname plugin in the first step. Next, we deactivate Autoptimize, Duo authentication plugin, W3TC and Opcache patch plugins.

All in all, WP-CLI is an incredibly useful tool for anyone who works with WordPress. It streamlines a lot of common tasks, making your life as a WordPress developer or site manager much easier.

  1. Clear disk space on CentOS 6 or CentOS 7— copy from a blog written by Danila Vershinin — just for archive — Please inform me to delete if … – Greatdolphin

    […] WP CLI¬†saves WordPress archives every time you setup a new WordPress website. You can remove those caches by the following command: […]


Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.