Skip to content
WP-CLI Tips That Actually Save Time

WP-CLI Tips That Actually Save Time

Al Amin Ahamed

Al Amin Ahamed

Senior Engineer

6 min read
𝕏 in

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

BASH
# 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

BASH
# 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

BASH
# 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:

PHP
/**
 * @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:

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

Now:

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

This alone saves me 10+ minutes per deployment.

Share 𝕏 in
Al Amin Ahamed

Al Amin Ahamed

Senior software engineer & AI practitioner. Laravel, PHP, WordPress plugins, WooCommerce extensions.

About me →

One email a month. No noise.

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