WP-CLI for working with WordPress

by , , revisited on

WP-CLI is excellent command line utility for WordPress websites. It is available on all GetPageSpeed WordPress servers.

Install WP-CLI

If your host is not GetPageSpeed, you can install WP-CLI using this 3 lines in bash:

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 working directory to the directory where you want to install 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 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.

  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

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