wordpress-auto-alt-text-code

WordPress Dev

Automatically Add Alt Text to Images in WordPress (SEO Boost 🚀)

Do you forget to write alt text when uploading images in WordPress?
That small step can have a big impact on your SEO!

When you skip alt text, Google can’t understand your images properly — meaning lost traffic from image search.

But don’t worry — with this simple PHP snippet, you can automatically add alt text to all your images using your post title.
No plugins, no extra setup — just clean, fast code!

💻 The Code Snippet

Here’s the full working snippet 👇

// 🖼️ Auto Add Alt Text from Post Title (Site-wide Ultra Short)

// For <img> in post content & widgets
foreach (['the_content','widget_text'] as $hook) {
    add_filter($hook, function($c){
        $t = is_singular() ? get_the_title() : get_bloginfo('name');
        return preg_replace(
            '/<img(?![^>]+alt=)([^>]+>)/i',
            '<img$1 alt="'.esc_attr($t).'">',
            $c
        );
    }, 12);
}

// For wp_get_attachment_image()
add_filter('wp_get_attachment_image_attributes', function($a,$img){
    if (empty($a['alt'])) {
        $a['alt'] = esc_attr(get_the_title($img->ID));
    }
    return $a;
}, 10, 2);

🧩 How to Use the Code

You can place this snippet in one of the following:

  1. Child theme’s functions.php file
  2. The Code Snippets plugin (recommended)
  3. Your custom plugin if you maintain one

⚠️ Never modify your main theme file — updates can overwrite it.

💡 How It Works

  • When an image tag <img> is found without an alt attribute, the script automatically adds one.
  • It uses:
    • The post title on single posts and pages.
    • The site name on archive or widget areas.
  • It also works for images fetched with wp_get_attachment_image().

This ensures every image has a meaningful alt tag — improving both SEO and accessibility.

🚀 Why You Should Add Alt Text Automatically

Alt text helps:

  • 🧠 Google understand your images (boosts SEO rankings)
  • 🦻 Screen readers describe content for accessibility
  • 📸 Image search visibility — alt text helps images appear in Google Image results

If you often forget to write alt text manually, this automation keeps your site SEO-friendly without extra effort.

⚙️ Customization Tips

You can adjust the snippet for your needs:

  • Replace get_the_title() with get_post_meta() if you want a custom field as alt text.
  • Add conditions for post types, e.g., only posts, not pages.
  • Use different alt formats: $a['alt'] = 'Image from ' . get_the_title($img->ID);

🌍 Bangla Tip for Local Developers

ছবি আপলোড করার সময় অনেকেই Alt Text লিখতে ভুলে যান,
এই কোডটি স্বয়ংক্রিয়ভাবে তোমার পোস্ট টাইটেল দিয়ে Alt Text বসিয়ে দেবে।
SEO ✅ Accessibility ✅
একবার বসালে পুরো সাইটে কাজ করবে!

✅ Conclusion

You don’t need plugins to manage alt text anymore.
This simple snippet automatically adds SEO-friendly alt text to every image in WordPress using your post title — clean, fast, and fully automated.

💡 A small tweak that makes a big SEO difference.

📘 FAQ

Q1: Does this add alt text to existing images?
Yes, it dynamically adds alt text when rendering images in posts, even old ones.

Q2: Will it overwrite existing alt text?
No. It only applies when an image doesn’t already have an alt attribute.

Q3: Is it safe for performance?
Yes. It uses lightweight WordPress filters — no extra queries or plugins.

Q4: Does it help SEO?
Absolutely! Google Image Search uses alt text heavily for ranking and discovery.

Related Topic: