Skip to content

WP-CLI Tips That Actually Save Time

6 min read

WP-CLI is the most underused tool in the WordPress ecosystem. Most developers know wp plugin install and wp db export. Here's what the power users are doing.

Scaffolding Commands

# Generate a complete plugin scaffold wp scaffold plugin my-plugin --plugin_name="My Plugin" --activate # Generate a custom post type with all the boilerplate wp scaffold post-type product --label="Product" --plugin=my-plugin # Generate a unit test for a class wp scaffold unit-tests my-plugin

Database Operations

# Search and replace URLs after a migration (handles serialized data correctly) wp search-replace 'https://staging.example.com' 'https://example.com' --precise --recurse-objects # Export a single table wp db export --tables=wp_options options.sql # Optimise all tables wp db optimize

User Management

# Create a one-time login link (if you have my wp-cli-magic-login package) wp magic-login generate admin # List all admins wp user list --role=administrator --fields=ID,user_login,user_email # Reset a password without knowing the old one wp user update 1 --user_pass=newpassword

Writing Custom Commands

Custom commands are where WP-CLI gets powerful. Here's a command to send a test notification to all subscribers:

/** * @when after_wp_load */ class TestNotificationCommand { public function __invoke(array $args, array $assoc_args): void { $users = get_users(['role' => 'subscriber', 'number' => -1]); $progress = WP_CLI\Utils\make_progress_bar('Sending', count($users)); foreach ($users as $user) { wp_mail($user->user_email, 'Test', 'This is a test.'); $progress->tick(); } $progress->finish(); WP_CLI::success(sprintf('Sent to %d users.', count($users))); } } WP_CLI::add_command('notify test', TestNotificationCommand::class);

Aliases for Multi-Environment Workflows

~/.wp-cli/config.yml:

alias: @prod: ssh: deployer@example.com path: /var/www/html @staging: ssh: deployer@staging.example.com path: /var/www/staging

Now:

wp @prod plugin list # runs on prod server wp @staging db export # exports staging DB

This alone saves me 10+ minutes per deployment.

Share X / Twitter LinkedIn
A

Al Amin Ahamed

Senior software engineer & AI practitioner. Building things in Laravel, PHP, and TypeScript.

About me β†’

One email a month. No noise.

What I shipped, what I read, occasional deep dive. Unsubscribe anytime.