# Fill Missing Alt Text in Bulk (Run Once)

> A one-off task that gives every image without alt text a description based on its title. Run it from the snippet editor whenever you want to catch up.

- Type: PHP
- Runs: On demand
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: alt-text, accessibility, bulk
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/bulk-fill-missing-alt-text/

## Code

```php
// Maximum number of images to update per run.
$wpide_alt_batch = 500;

if ( ! current_user_can( 'upload_files' ) && ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
    return;
}

$wpide_images = get_posts( array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'fields'         => 'ids',
    'numberposts'    => $wpide_alt_batch,
    'meta_query'     => array(
        'relation' => 'OR',
        array( 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS' ),
        array( 'key' => '_wp_attachment_image_alt', 'value' => '' ),
    ),
) );

$wpide_updated = 0;

foreach ( $wpide_images as $wpide_image_id ) {
    $wpide_alt = preg_replace( '/\b(img|dsc|dscn|pxl|image|photo|screenshot)[\s_\-]*\d+\b/i', '', (string) get_post_field( 'post_title', $wpide_image_id ) );
    $wpide_alt = trim( preg_replace( '/[\s_\-]+/', ' ', $wpide_alt ) );

    if ( '' === $wpide_alt ) {
        continue;
    }

    update_post_meta( $wpide_image_id, '_wp_attachment_image_alt', sanitize_text_field( ucfirst( $wpide_alt ) ) );
    $wpide_updated++;
}

echo esc_html( sprintf( 'Alt text added to %1$d of %2$d images without it.', $wpide_updated, count( $wpide_images ) ) );
```

## Notes

Processes up to 500 images per run; run it again for more. Review important images afterwards, as titles are not always good descriptions.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Fill Missing Alt Text in Bulk (Run Once)" 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/
