-
-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Description
I can't think of many use cases for having different alt-text for different image sizes, or for different aspect-ratios, for that matter. Therefore I'd suggest adding a hook directly to wp_get_attachment_image_attributes that checks for missing alt-text and attempts to set the alt-text directly so the image tag displays as expected.
Below is an example of such a function:
/**
* Checks for a missing or empty alt tag and attempts to find the alt text
* from the "original image" meta value provided by the ACF Image Aspect Ratio Crop plugin
*/
add_filter('wp_get_attachment_image_attributes', function ($attr, $attachment) {
if (!isset($attr['alt']) || empty($attr['alt'])) {
// attempt to get the ID of the original image which may contain the original alt text
$original_id = get_post_meta($attachment->ID, 'acf_image_aspect_ratio_crop_original_image_id', true);
$original_alt = get_post_meta($original_id, '_wp_attachment_image_alt', true);
// assign this value to the alt key (will be empty if either get_post_meta call above fails)
$attr['alt'] = $original_alt;
}
return $attr;
}, 10, 2);Metadata
Metadata
Assignees
Labels
No labels