Fix image-alt in WordPress
WordPress themes frequently print hardcoded <img src='...'> tags in custom templates or output empty alt attributes when editors skip the Media Library Alternative Text field. Using wp_get_attachment_image() with a clean wp_get_attachment_image_attributes fallback prevents blank alt attributes across every archive and single post.
Before and after
<?php // template-parts/content-card.php (Broken: raw <img> with no alt attribute) ?>
<?php $thumb_url = get_the_post_thumbnail_url( get_the_ID(), 'medium_large' ); ?>
<?php if ( $thumb_url ) : ?>
<img src="<?php echo esc_url( $thumb_url ); ?>" class="post-card__img" loading="lazy">
<?php endif; ?><?php // template-parts/content-card.php (Fixed: wp_get_attachment_image + safe alt fallback) ?>
<?php
$thumb_id = get_post_thumbnail_id( get_the_ID() );
if ( $thumb_id ) {
$stored_alt = trim( (string) get_post_meta( $thumb_id, '_wp_attachment_image_alt', true ) );
$safe_alt = '' !== $stored_alt ? $stored_alt : get_the_title();
echo wp_get_attachment_image(
$thumb_id,
'medium_large',
false,
array(
'class' => 'post-card__img',
'loading' => 'lazy',
'alt' => esc_attr( $safe_alt ),
)
);
}
?>Fixed vs. broken example
Always provide meaningful alt text for informative photos and explicitly set alt="" (empty string) for purely decorative flourishes so screen readers skip them cleanly.
Playwright + axe-core check
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
test("WordPress post thumbnails and archive images pass image-alt", async ({ page }) => {
await page.goto("/blog/");
const results = await new AxeBuilder({ page })
.withRules(["image-alt"])
.analyze();
expect(results.violations).toEqual([]);
});Template deduplication tip
A single custom template part like template-parts/content-card.php missing an alt attribute triggers image-alt on every WordPress blog archive and category page. A11yDrift deduplicates those into one WordPress template cluster.
What NVDA and VoiceOver announce
When an informative image lacks an alt attribute, screen readers either skip it silently or read out raw CDN filenames and hashes, blocking blind shoppers and readers from understanding products, charts, and editorial photos.
NVDA (Windows)
"graphic, cdn_shop_files_IMG_8842_v1731942.webp" (or skipped silently depending on verbosity)
"Cobalt blue stoneware ceramic pour-over mug with matte handle, graphic"
VoiceOver (macOS / iOS)
"IMG_8842_v1731942.webp, image"
"Cobalt blue stoneware ceramic pour-over mug with matte handle, image"
Common questions
Why does WordPress sometimes render alt="" even after I fill in the Media Library?
Should I set every missing WordPress image alt to the filename?
How do I handle decorative background flourishes in WordPress block themes?
Test a URL for image-alt
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Managing WordPress client sites? Monitor 15 sites on one flat plan with white-label portals and branded executive PDFs—$1,140/yr ($95/mo billed annually, 24-month rate lock + onboarding) for Founding 10 agencies.