Adding alt tag information to every product photo is a lot of work. We always just copy and paste our product title into the image alt tags.

I figured since all the information is there; there must be a way to do this automatically.

Question: How do you apply a woocommerce product’s TITLE as all the ALT TAGS for images used with that product.

Any help is much appreciated!

3 Answers
3

This is what you need, taken from – https://stackoverflow.com/questions/27087772/how-can-i-change-meta-alt-and-title-in-catalog-thumbnail-product-thumbnail

add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);

function change_attachement_image_attributes( $attr, $attachment ){
    // Get post parent
    $parent = get_post_field( 'post_parent', $attachment);

    // Get post type to check if it's product
    $type = get_post_field( 'post_type', $parent);
    if( $type != 'product' ){
        return $attr;
    }

    /// Get title
    $title = get_post_field( 'post_title', $parent);

    $attr['alt'] = $title;
    $attr['title'] = $title;

    return $attr;
}

Leave a Reply

Your email address will not be published. Required fields are marked *