How to install and configure WP-CLI? 

What is WP-CLI?

WP-LI (WordPress Command Line Interface) is a command line tool that will allow you to interact and manage your WordPress site more efficiently. 

With WP-CLI, developers and administrators can perform many tasks, such as installing and updating plugins, creating content, recovering lost passwords, managing users and much more, without needing to access the WordPress admin panel.

How does WP-CLI work?

WP-CLI works by executing commands in the already installed console, and these interact with your WordPress site. Each command (https://developer.wordpress.org/cli/commands/) is designed to perform a specific task and can be combined with additional options and arguments to customize the operation. 

By working directly with the WordPress file system and database, WP-CLI can perform quick and automatic operations, allowing you to manage multiple sites or perform repetitive tasks.

Installing and Configuring WP-CLI

Before you begin, make sure your system meets these requirements:

  • UNIX-like operating system (OS X, Linux, FreeBSD, Cygwin); Windows support is limited.
  • PHP 5.6 or later version.
  • WordPress 3.7 or later (earlier versions may not work properly).

Once you have verified the requirements, download the wp-cli.phar file with wget or curl: 

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

Afterwards, make sure that the Phar archive is working correctly:

php wp-cli.phar --info

To use WP-CLI with the WP command from the command line, make the file executable and move it to a directory that is in your PATH. For example:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

If the installation of WP-CLI was successful, you should see information similar to this when you run wp –info:

$ wp --info
OS: Linux 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64
Shell: /usr/bin/zsh
PHP binary: /usr/bin/php
PHP version: 8.0.5
MySQL binary: /usr/bin/mysql
php.ini used: /etc/php/8.0/cli/php.ini
MySQL version: mysql Ver 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
SQL modes:
WP-CLI root dir: /home/wp-cli/
WP-CLI vendor dir: /home/wp-cli/vendor

WP_CLI phar path:

WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config:
WP-CLI project config: /home/wp-cli/wp-cli.yml
WP-CLI version: 2.9.0

Most Frequent WP-CLI Commands

  1. Installing and Upgrading WordPress
  • Install wordpress: 
wp core download
wp config create --dbname=database_name --dbuser=user --dbpass=password --dbhost=localhost
wp core install --url=https://tusitio.com --title=“Site Title” --admin_user=admin --admin_password=password [email protected]
  • Update WordPress:
wp core update
wp core update-db
  1. Plugin Management
  • Install a Plugin: 
wp plugin install plugin_name --activate
  • Update All Plugins:
wp plugin update --all
  • Deactivate a Plugin:
wp plugin deactivate plugin_name --activate plugin_name
  1. Managing Themes
  • Install a Theme:
wp theme install theme_name --activate
  • Update All Themes:
wp theme update --all
  • Activate a Theme:
wp theme activate theme_name
  1. User Management
  • Create a User:
wp user create user_name [email protected] --role=author --user_pass=password
  • Change the Role of a User:
wp user set-role user_name editor
  • Delete a User:
wp user delete user_name
  1. Content Management
  • Create a Post:
wp post create --post_type=post --post_title=“Post Title” --post_status=publish --post_content="Post Content.”
  • Update an Entry:
wp post update ID --post_title=“New Title” --post_content="New content.”
  • Delete an Entry:
wp post delete ID

WP-CLI is an indispensable and flexible tool that can save you time and effort in managing WordPress sites.