Wordpress CLI
Manage Wordpress from the command line


Tasks such as changing the URL or the name of the administrator are sometimes not so easy to perform when managing a website based on Wordpress. Historically, the process was to access the database (MySQL or MariaDB), log in, and enter some SQL statements. This is something that we still need to do sometimes, but let’s accept that it carries the risk of leaving the database in an inconsistent or unusable state. Obviously, the recommendation is to make a backup before manipulating the database.
There is a better, safer, and more compatible way with your Wordpress installation: WP-CLI, the official command line interface for Wordpress.
What is Wordpress CLI or WP-CLI?
Wordpress CLI or WP-CLI is a command line interface, which means you interact with your Wordpress installation from a terminal in text mode to perform administrative tasks.
WP-CLI was introduced by Automattic (the company behind Wordpress) in 2011. After several years in development, it achieved its stable version 1.0.0 in 2016. Since then, it has become a standard in managing Wordpress installations.
What do I need to use it?
We don’t want to mislead you; you need to have some advanced knowledge. This is because you will need to feel comfortable using a Unix environment (MacOS, Linux, etc.). Nevertheless, we recommend that you get to know it if you consider advancing in the advanced management of Wordpress environments.
Here are the minimum requirements to get started:
- Unix environment (MacOS, Linux, Cygwin, FreeBSD)
- PHP 5.6 or higher installed
- Wordpress 3.7 or higher (Although some older versions can be used, we do not recommend it for security reasons)
How can I install it?
Using your Unix terminal
To avoid repeating the installation guide here, we link to the official guide, which is very straightforward to follow.
Using Docker
Another option, if you use Docker for your Wordpress installation, is to also use Docker to launch WP-CLI. Imagine you have two services running using Docker, one for the MariaDB database and another for Wordpress itself. An example of these services with Docker Compose would be:
version: '3.9'
services:
wordpress:
image: wordpress:latest
container_name: wordpress
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: wp_pass
WORDPRESS_DB_NAME: wp_database
volumes:
- wordpress_data:/var/www/html
depends_on:
- db
db:
image: mariadb:latest
container_name: mariadb
environment:
MYSQL_DATABASE: wp_database
MYSQL_USER: wp_user
MYSQL_PASSWORD: wp_pass
MYSQL_ROOT_PASSWORD: root_pass
volumes:
- db_data:/var/lib/mysql
volumes:
wordpress_data:
db_data:
Now you can have a service for WP-CLI, for example using this configuration:
wpcli:
depends_on:
- wordpress
- db
image: wordpress:cli
links:
- db
entrypoint: wp
command: "--info"
volumes:
- wordpress-data:/var/www/html
working_dir: /var/www/html
With this configuration, you can launch commands using docker directly:
docker-compose run --rm wpcli <COMMAND>
Basic usage guide
Now let’s move on to the most interesting part: how to use WP-CLI. It is a very powerful tool, so let’s give a basic overview of typical use cases.
First, let’s see how to use it. Let’s first launch its help in line:
wp help
You should now see all the commands you can run with WP-CLI. If you want to know more about a specific command:
wp help <command>
The structure for launching commands is always the same:
wp <command> <subcommand> [arguments] [options]
The most commonly used commands are:
- wp core for main Wordpress administration tasks, such as configuring or updating the installation:
- Example:
wp core update
- Example:
- wp plugin for managing plugins. For example, installing, activating, deactivating, or listing active plugins.
- Example:
wp plugin install akismet --activate
to install and activate the Akismet plugin.
- Example:
- wp theme for handling everything related to Wordpress themes, such as installing or activating them:
- Example:
wp theme install twentytwenty --activate
to install and activate the Twenty Twenty theme.
- Example:
- wp post for managing posts, such as creating, updating, or deleting them.
- Example:
wp post create --post_title="Welcome to Wordpress" --post_status=publish
to create a new post and publish it.
- Example:
- wp user to manage user accounts, being able to list, create, update, or delete them.
- Example:
wp user list
to list all registered users.
- Example:
- wp db for database management; remember to use it with caution. You can perform tasks such as exporting the database, importing it, or executing queries, which is perfect for backups and migrations.
- Example:
wp db export
to create a backup of your database.
- Example:
There are many more commands that are very useful; for instance, another very useful use case is changing the title of your Wordpress site by executing wp option update blogname "My Blog"
.
We hope that this brief guide has helped you start or expand your knowledge on other ways to use WP-CLI, such as using Docker.
At Sitecloud, we use WP-CLI internally to perform the most common administrative tasks like generating backups or updating the Wordpress version, so you won’t need to worry about anything. Nevertheless, it is always useful to learn how to use the full power of WP-CLI.