WP-CLI
Overview
WP-CLI (WordPress Command Line Interface) lets you manage a WordPress installation directly from the terminal. Instead of using the browser-based admin dashboard, you execute commands via SSH or a local terminal.
It supports:
Core management (install, update, downgrade)
Plugin and theme management
Database operations
User and role management
Content import/export
Custom command development
WP-CLI commands follow this structure:
wp <entity> <subcommand> [options] [--global-parameters]
Example:
wp core update
Installation (Quick Method)
Download and make executable:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Verify installation:
wp --info
Built-in Command Examples
Core Management
Install WordPress:
wp core install \
--url="example.com" \
--title="My Site" \
--admin_user="admin" \
--admin_password="password" \
--admin_email="admin@example.com"
Update WordPress:
wp core update
Downgrade to WordPress 6.3:
wp core update --version=6.3 --force
Plugin Management
Install and activate a plugin:
wp plugin install woocommerce --activate
Deactivate a plugin:
wp plugin deactivate akismet
Update all plugins:
wp plugin update --all
Theme Management
Install and activate theme:
wp theme install twentytwentyfour --activate
List installed themes:
wp theme list
Post Management
Create a post:
wp post create \
--post_title="CLI Post" \
--post_status=publish \
--post_author=1
List posts from specific user:
wp post list --author=1
Delete a post:
wp post delete 123 --force
User Management
Create a user:
wp user create editor editor@example.com --role=editor
List users:
wp user list
Change user role:
wp user set-role 5 administrator
Database Operations
Export full database:
wp db export
Export specific tables:
wp db export --tables=wp_options,wp_users
Import database:
wp db import database.sql
Global Parameters
Global parameters apply to most commands.
Run command for a specific path:
wp plugin list --path=/var/www/html
Run command as a specific user:
wp post list --user=admin
Skip plugins:
wp option get siteurl --skip-plugins
Logging Output in WP-CLI
Redirect output to file:
wp plugin list > plugins.txt
Append output:
wp user list >> users.log
Enable debug mode:
wp plugin update --all --debug
Creating a Custom Command
Create a simple custom command in a plugin:
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'custom greet', function( $args ) {
$name = $args[0] ?? 'User';
WP_CLI::success( "Hello, $name!" );
} );
}
Usage:
wp custom greet John
Adding a Subcommand to Existing Command
Example: Add subcommand to wp user
WP_CLI::add_command( 'user promote_all', function() {
$users = get_users();
foreach ( $users as $user ) {
$user->set_role( 'editor' );
}
WP_CLI::success( 'All users promoted to editor.' );
});
Usage:
wp user promote_all
Help Command
Get command documentation directly in terminal:
wp help
Help for specific command:
wp help plugin
Help for subcommand:
wp help db export