How to Automate Routine Tasks in WordPress Using the wp-cli Console Utility

How to Automate Routine Tasks in WordPress Using the wp-cli Console Utility

An important “non-news” for WordPress site developers. Have you heard about the command-line utility called “wp-cli”? I’ve been working with WP for a long time, but only found out about it about six months ago.

wp-cli is an amazing tool for developers who manage a bunch of WP sites on their local machine (and not just local ones). Wp-cli allows you to work with WordPress via the command line, minimizing the time you spend on installing the engine, updating it, as well as installing and updating themes and plugins for it.
To be honest, I haven't explored all of its features deeply, but I'll describe the basic functionality and capabilities.

Installation

All instructions are provided for Linux OS.
Download the phar archive with the utility to your machine:

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

Set execute permissions:

chmod +x wp-cli.phar

Move the file to your system’s program directory (to access it from anywhere):

sudo mv wp-cli.phar /usr/local/bin/wp

How to check if it works? Run the following command in the console:

wp --info

And if everything is installed correctly, you'll see something like this:

PHP binary: /opt/lampp/bin/php-5.5.19
PHP version: 5.5.19
php.ini used: /opt/lampp/etc/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /www/cms-free/wordpress/test
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 1.4.0

Commands

Check the installed version of WP. Go to the directory where WP is installed and enter:

wp core version --extra

Installing WordPress takes just three commands:
1. Download the core with the required locale:

wp core download --locale=ru_RU

2. Before installing the site, you need to create a “wp-config.php” file by renaming “wp-config-sample.php” and setting your database credentials. Then run:

wp core install --url=example.com --title=Development --admin_user=admin --admin_password=qweqwe --admin_email=admin@example.com

where:
--url — site URL
--title — site title
--admin_user — admin username
--admin_password — admin password
--admin_email — admin email

In just a few seconds, WP will be installed!

To update WordPress core:

wp core update

Themes and Plugins

View installed plugins and themes:

wp plugin list
wp theme list

Update all plugins and themes:

wp plugin update --all
wp theme update --all

Install the WooCommerce plugin and the TwentyTwelve theme:

wp plugin install woocommerce
wp theme install twentytwelve

Populate the site with test posts from loripsum:

curl http://loripsum.net/api/5 | wp post generate --post_content --count=10

As you can see, it’s all pretty simple and fast. The main thing is to get started. Thank you!

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

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