If you’re like me you’re disappointed that Divi does not grab the ALT information for images from the Media Library.
ALT tags are an important component of on-page SEO.
The solution is to use the following code in your child theme’s functions.php:
// Fetch image alt text from media library
function get_image_alt_text($image_url) {
if ( ! $image_url ) return '';
if ( '/' === $image_url[0] )
$post_id = attachment_url_to_postid(home_url() . $image_url);
else
$post_id = attachment_url_to_postid($image_url);
$alt_text = get_post_meta($post_id, '_wp_attachment_image_alt', true);
if ( '' === $alt_text )
$alt_text = get_the_title($post_id);
return $alt_text;
}
/* Update image alt text in module properties */
function update_module_alt_text( $attrs, $unprocessed_attrs, $slug ) {
if ( ( $slug === 'et_pb_image' || $slug === 'et_pb_fullwidth_image' ) && '' === $attrs['alt'] )
$attrs['alt'] = get_image_alt_text($attrs['src']);
elseif ( $slug === 'et_pb_blurb' && 'off' === $attrs['use_icon'] && '' === $attrs['alt'] )
$attrs['alt'] = get_image_alt_text($attrs['image']);
elseif ( $slug === 'et_pb_slide' && '' !== $attrs['image'] && '' === $attrs['image_alt'] )
$attrs['image_alt'] = get_image_alt_text($attrs['image']);
elseif ( $slug === 'et_pb_fullwidth_header' ) {
if ( '' !== $attrs['logo_image_url'] && '' === $attrs['logo_alt_text'] )
$attrs['logo_alt_text'] = get_image_alt_text($attrs['logo_image_url']);
if ( '' !== $attrs['header_image_url'] && '' === $attrs['image_alt_text'] )
$attrs['image_alt_text'] = get_image_alt_text($attrs['header_image_url']);
}
return $attrs;
}
/* Filter injection */
add_filter( 'et_pb_module_shortcode_attributes', 'update_module_alt_text', 20, 3 );
Grab the free e-book "How to Write Copy That People AND Search Engines LOVE"
If you would like some more information on how to leverage the latest trends for your business, contact Steve at insight.web@insightdesign.com.au or call
.
Connect
Thank you SO much! Alt tags are absolutely important for the needs of blind and visually impaired people. Divi should find a way to incorporate their use in their theme yesterday.
You’re very welcome!
Thanks for the snippet! A Shame that Divi is managing the alt and title tags in that way.
You’re welcome Rafael
Hi folks.
We built on the above idea. Our version takes whatever is in the library at the point of page render – meaning the library is always the best place to store things.
AND it puts an alt tag in even if none is found:
https://practically.io/thinking/divi-alt-tags-done-right-in-functions-php/
Hi Sam, Steve,
This is really useful – thank you both! Would it be possible to add lines to your scripts to update the title as well so when an image title is updated in the Media Library it’s also reflected in the title of the image on each page?
Best,
Matthew
P.S. There’s a free plugin from Pee Aye that adds Captions to Divi’s Image Module (https://www.peeayecreative.com/product/divi-image-helper/). If the scripts could also support adding caption info too that would be really helpful.