# Product Name as Alt Text for WooCommerce Images Without One

> When a product image in the shop, on category pages or on the product page has no alternative text, uses the product name instead of leaving it empty.

- Type: PHP
- Runs: Frontend only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Requires: WooCommerce
- Tags: woocommerce, alt-text, images
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-woo-product-image-alt/

## Code

```php
add_filter( 'wp_get_attachment_image_attributes', function ( $attr, $attachment ) {
    if ( '' !== trim( (string) ( isset( $attr['alt'] ) ? $attr['alt'] : '' ) ) || ! function_exists( 'wc_get_product' ) ) {
        return $attr;
    }

    $post_id = get_the_ID();

    if ( $post_id && 'product' === get_post_type( $post_id ) ) {
        $attr['alt'] = wp_strip_all_tags( get_the_title( $post_id ) );
    } elseif ( $attachment && $attachment->post_parent && 'product' === get_post_type( $attachment->post_parent ) ) {
        $attr['alt'] = wp_strip_all_tags( get_the_title( $attachment->post_parent ) );
    }

    return $attr;
}, 20, 2 );
```

## Notes

Images with their own alt text in the media library are not changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Product Name as Alt Text for WooCommerce Images Without One" 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/
