Automatically Sync Divi Image Module Alt and Title Fields with the Media Library

Written by Dan Mossop

In this post, we'll cover how to dynamically sync the alt and title attributes of the Divi Image Module with the information stored in the WordPress Media Library. This enhances accessibility and SEO by ensuring the correct use of image attributes.

While Divi will retrieve the Image Module’s alt text and title fields when the image is first added, it doesn't keep them synchronized with the Media Library as the alt / title text are added or changed. Users have to manually update these attributes, which can be cumbersome. 

Furthermore, if you're adding images using dynamic content (e.g. from an ACF field), then the image attributes are not retrieved by Divi at all. 

Automatically Sync Divi Image Module Alt and Title Fields with the Media Library using PHP

Here's a PHP solution that ensures the alt and title attributes of images in the Divi Image Module are automatically updated based on the Media Library settings. This code can be added to your child theme's functions.php file or via a plugin like Code Snippets.

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;
}

By adding this PHP code, you ensure the alt and title fields of images in the Divi Image Module are dynamically kept in sync with the Media Library, improving accessibility and SEO. This method simplifies the process and maintains consistent image attributes across your WordPress site.

Streamline PHP Code Integration with Divi!

Effortlessly add and manage PHP code in your Divi layouts with the Divi PHP Code Module. This tool enables dynamic functionality and automation, enhancing your site's performance and SEO. Perfect for integrating solutions like syncing image attributes with the Media Library.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. Thanks for this piece of code. It saved me half an hour. :)

    Reply
    • Hey Kris, glad to hear it helped! If there's anything else you need help with, just let me know. Cheers!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.

Latest Posts

How to Hide a Divi Module When Scrolling Up or Down

In this quick tutorial, I’ll show you how to hide a Divi module when scrolling in a specific direction (up or down), so that the module only shows when you want it to. This is especially useful when using Divi’s Scroll Effects feature and you have a effect you want to...

Fade a Divi Image Module Edge into the Background

Want to create a stylish fade effect on your Divi image module—where one side fades smoothly into the background? With a bit of CSS, you can make any edge (or corner) of the image fade out: top, bottom, left, right, or even diagonally.Fade a Divi Image Module Edge...

Hide the Header and Footer in the Hello Elementor Theme

Removing the default header and footer from your Hello Elementor theme allows for a streamlined and distraction-free website design. This is especially useful when creating unique landing pages, full-width layouts, or custom headers and footers with a page builder. In...

Setting up the Divi Password Box Module

Setting up password protection on a page can help you control access to sensitive or private content in WordPress, allowing only authorized visitors to view certain sections. With the Divi Password Box module, you can replace the plain Divi password form with a fully...

Open Divi Social Media Icons in a New Tab

Ensuring your website's social media icons open in a new browser tab creates a seamless user experience and keeps visitors engaged with your site while allowing them to explore your social media presence. This approach prevents users from navigating away from your...