Divi copies image meta data onto the ‘advanced’ tab of the Divi image module when the image is selected, but it does not update the module data when you change the meta data in the media library. The following script keeps tha alt and title attributes of images in the Divi Image Module in sync with the media library. Add it to the cuntions.php file of your child theme.
add_filter('et_pb_module_shortcode_attributes', 'db_add_image_alt_and_title', 10, 3);
function db_add_image_alt_and_title($attrs, $content, $module_slug) {
if ($module_slug !== 'et_pb_image' || empty($attrs['src'])) {
return $attrs;
}
// Look up the image attachment id
$image_id = attachment_url_to_postid($attrs['src']);
if (!$image_id) {
return $attrs;
}
// Retrieve the alt text
$alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
if (!empty($alt_text)) {
$attrs['alt'] = $alt_text;
}
// Retrieve the title text
$title_text = get_the_title($image_id);
if (!empty($title_text)) {
$attrs['title_text'] = $title_text;
}
return $attrs;
}
Code language: PHP (php)