Custom Code Snippet to Automatically Add Alt Text to Images in WordPress

Marek T Jan 28, 2025 SEO and Accessibility
Is there a way to make sure all my pictures have descriptions without me having to do it one by one?
Can I use a custom code snippet to automatically generate and add alt text to images in WordPress for better SEO and accessibility?
Andy answered Jan 28, 2025

Solution: Automatic Alt Text Generation for WordPress Images

Custom Code Solution

This code automatically generates alt text for images when they're uploaded to WordPress. It uses the image filename (cleaned up) as a fallback and can be enhanced with AI services.

Add this code to your theme's functions.php file or in a site-specific plugin:

Basic implementation using filename as alt text:

function auto_add_image_alt($post_ID) {
    if (wp_attachment_is_image($post_ID)) {
        $image_title = get_post($post_ID)->post_title;
        
        // Clean up filename to create readable alt text
        $alt_text = preg_replace('/\.(jpg|jpeg|png|gif)$/i', '', $image_title);
        $alt_text = str_replace(['-', '_'], ' ', $alt_text);
        $alt_text = ucwords($alt_text);
        
        update_post_meta($post_ID, '_wp_attachment_image_alt', $alt_text);
    }
}
add_action('add_attachment', 'auto_add_image_alt');

Advanced implementation with multiple fallback options:

function enhanced_auto_alt_text($post_ID) {
    if (!wp_attachment_is_image($post_ID)) return;
    
    $image = get_post($post_ID);
    $alt_text = '';
    
    // Priority order for alt text
    if (!empty($image->post_excerpt)) {
        $alt_text = $image->post_excerpt;
    } elseif (!empty($image->post_title)) {
        $alt_text = preg_replace('/\.(jpg|jpeg|png|gif)$/i', '', $image->post_title);
        $alt_text = str_replace(['-', '_'], ' ', $alt_text);
        $alt_text = ucwords($alt_text);
    } elseif (!empty($image->post_content)) {
        $alt_text = wp_trim_words($image->post_content, 10);
    }
    
    if (!empty($alt_text)) {
        update_post_meta($post_ID, '_wp_attachment_image_alt', $alt_text);
    }
}
add_action('add_attachment', 'enhanced_auto_alt_text');

Plugin Solutions

If you prefer a plugin solution, here are reliable options:

  1. Auto Image Alt Text - Uses AI to generate descriptive alt text
  2. SEO Friendly Images - Automatically adds alt and title attributes
  3. EWWW Image Optimizer - Includes alt text generation among other features

Important Notes

  • The custom code solutions provided will only work for newly uploaded images
  • For existing images, you'll need to run a database update script
  • AI-based solutions typically require API keys and may have associated costs
  • Consider combining automatic generation with manual review for important images

To update existing images, you can add this code temporarily and run it once:

function update_existing_images_alt() {
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_mime_type' => 'image'
    );
    
    $images = get_posts($args);
    
    foreach($images as $image) {
        if (!get_post_meta($image->ID, '_wp_attachment_image_alt', true)) {
            enhanced_auto_alt_text($image->ID);
        }
    }
}
// Uncomment to run:
// add_action('init', 'update_existing_images_alt');

Remove or comment out the action after running it once to avoid unnecessary processing.