Skip to content

Add WP-CLI command for bulk alt text generation #435

@adamsilverstein

Description

@adamsilverstein

What problem does this address?

The alt text generation feature currently requires manual interaction — either one image at a time via the editor/media modal, or selecting images in the Media Library bulk action (which processes sequentially in the browser tab). For sites with large existing media libraries (hundreds or thousands of images missing alt text), there's no efficient way to backfill.

Common scenarios where this matters:

  • Migration: A site migrating to WordPress that has years of images without alt text
  • Accessibility audits: Remediating an existing site to meet WCAG compliance
  • Ongoing maintenance: Processing new uploads added by contributors who skip alt text

What is your proposed solution?

Add a wp ai alt-text generate WP-CLI command — the plugin's first CLI integration. It queries images missing alt text and processes them server-side in batches using the existing ai/alt-text-generation ability.

Command usage

# Generate alt text for all images missing it
wp ai alt-text generate

# Preview what would be processed
wp ai alt-text generate --dry-run

# Regenerate for specific images (even if alt text exists)
wp ai alt-text generate --ids=42,55,100 --force

# Process in small batches with custom delay
wp ai alt-text generate --batch-size=5 --delay=1000

Flags

Flag Type Default Description
--batch-size int 20 Images per batch
--dry-run flag false Preview without changes
--force flag false Regenerate even if alt text exists
--ids string Comma-separated attachment IDs
--delay int 500 Milliseconds between API calls

Implementation approach

  • New file: includes/CLI/Alt_Text_Command.php — the command class, under the WordPress\AI\CLI namespace (auto-resolved by the existing PSR-4 autoloader)
  • Modified file: includes/Main.php — register the CLI command conditionally when WP_CLI is defined
  • Reuses the existing ability: calls wp_get_ability('ai/alt-text-generation')->execute() for each image, same as the REST endpoint
  • Saves via update_post_meta() directly — no REST overhead
  • Memory-safe: clears object cache between batches to handle large libraries
  • Progress bar: uses WP_CLI\Utils\make_progress_bar() for real-time feedback
  • Summary table: reports generated, decorative, skipped, and failed counts at the end
  • Handles decorative images: saves empty alt text so they aren't reprocessed

Pre-flight checks

The command validates before processing:

  1. The ai/alt-text-generation ability is registered (experiment must be enabled)
  2. AI credentials are configured

This also opens the door for future CLI commands (e.g., wp ai excerpt generate, wp ai title generate) using the same pattern.

Metadata

Metadata

Labels

No labels
No labels
No fields configured for Enhancement.

Projects

Status

Needs review

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions