# Report Images Missing Alt Text (WP-CLI)

> Adds a "wp missing-alt" command that lists every image in the Media Library without alt text, with the post it is attached to, so you can fix accessibility gaps in order of importance.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: wp-cli, media, accessibility
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cli-missing-alt-report/

## Code

```php
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
    return;
}

WP_CLI::add_command( 'missing-alt', function ( $args, $assoc_args ) {
    global $wpdb;

    $rows = $wpdb->get_results(
        "SELECT p.ID, p.post_title, p.post_parent FROM {$wpdb->posts} p
        LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_wp_attachment_image_alt'
        WHERE p.post_type = 'attachment' AND p.post_mime_type LIKE 'image/%' AND ( m.meta_value IS NULL OR m.meta_value = '' )
        ORDER BY p.post_parent DESC, p.ID DESC"
    );

    if ( ! $rows ) {
        WP_CLI::success( 'Every image has alt text.' );
        return;
    }

    $items = array();
    foreach ( $rows as $row ) {
        $items[] = array(
            'ID'          => $row->ID,
            'title'       => $row->post_title,
            'file'        => wp_basename( (string) get_attached_file( $row->ID ) ),
            'attached_to' => $row->post_parent ? get_the_title( $row->post_parent ) . ' (#' . $row->post_parent . ')' : '-',
        );
    }

    WP_CLI\Utils\format_items( $assoc_args['format'] ?? 'table', $items, array( 'ID', 'title', 'file', 'attached_to' ) );
}, array(
    'shortdesc' => 'Lists images that have no alt text.',
    'synopsis'  => array(
        array( 'type' => 'assoc', 'name' => 'format', 'optional' => true, 'default' => 'table', 'options' => array( 'table', 'csv', 'json', 'count', 'ids' ) ),
    ),
) );
```

## Notes

Use --format=csv > missing-alt.csv to hand the list to an editor.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Report Images Missing Alt Text (WP-CLI)" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
