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';"
mysql -e "FLUSH PRIVILEGES;"
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.