How to Use WP-CLI to Manage WordPress
WP-CLI is a powerful command-line tool that lets you manage a WordPress site without using a browser. With it you can quickly update plugins, work with posts, manage users, make backups and automate routine tasks.
It is ideal for both developers and administrators, especially if you work with several sites or large projects.
Why You Should Use WP-CLI
In everyday work with WordPress most actions are done through the admin area: you update plugins, install themes, create pages or users, clean the database and so on.
With WP-CLI you can do all this several times faster by simply typing commands in the terminal. This is convenient if you:
- maintain several sites and want to speed up routine operations;
- automate processes (scripts, cron);
- work on staging or production servers via SSH, where there is no access to the admin area or where you need to solve problems quickly;
- want to perform bulk operations without clicking through the interface.
WP-CLI is also ideal for local development: you can test commands on a local server (for example, WAMP) without the risk of damaging a live site. All commands will be executed on your computer, not on a real server.
Installing WP-CLI on a Local Server (WAMP)
For convenience, create a separate folder for WP-CLI, for example C:\wp-cli. This lets you use WP-CLI globally and not tie it to a specific site. In the command line (Command Prompt), create the folder and go into it:
mkdir C:\wp-cli cd C:\wp-cli
While in this folder, download WP-CLI with the command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Sometimes the downloaded file may have the extension .txt, for example wp-cli.phar.txt. In this case rename it back to wp-cli.phar:
rename wp-cli.phar.txt wp-cli.phar
You can check that WP-CLI works with the command:
php C:\wp-cli\wp-cli.phar --info
This command will output information about the version of WP-CLI, PHP and your working environment.
So that you do not have to type the full path to the .phar every time, you can create a batch file wp.bat in the same folder with the contents:
@ECHO OFF php "C:\wp-cli\wp-cli.phar" %*
After that you can run the command simply as:
Connect to the server via SSH:
php wp-cli.phar --info
Working with WP-CLI
WP-CLI should always be run from the site root, where wp-config.php is located.
On a local Windows server this is usually:
C:\wamp64\www\mysite
On a live server via SSH:
cd /path/to/wordpress
*Replace mysite or /path/to/wordpress with the real names of your folders.
Useful WP-CLI Commands
Updating the WordPress Core
wp core update
Installing and Activating a Plugin
wp plugin install contact-form-7 --activate
*Replace contact-form-7 with the name of your plugin.
Installing and Activating a Theme
wp theme install twentytwentyfive --activate
*Replace twentytwentyfive with the theme you need.
Adding a New User
wp user create editor [email protected] --role=editor --user_pass=123456
*Replace editor, [email protected] and 123456 with the real username, email and password you want to use.
Database Backup
wp db export backup.sql
Database Import
wp db import backup.sql
Deactivating a Faulty Plugin
wp plugin deactivate problem-plugin
*Replace problem-plugin with the name of your plugin.
Summary
Using WP-CLI lets you:
- speed up WordPress management;
- safely update plugins, themes and the core;
- manage users and the database directly through the terminal;
- automate repetitive tasks;
- quickly solve problems with plugins or other components without logging in to the admin area.
These capabilities make WP-CLI especially useful for developers, administrators and when working with several sites or large projects.
Our specialization is WordPress website development and maintenance. Contact us for a free consultation — [email protected], +371 29394520
