Copy Image Title and Alt Text From Media Library to Posts

Say you have a website where you have manually updated the image attributes of some of the images in the media library. Now you want to copy them to the posts (or products) where the images are used.

If you haven’t noticed already, updating image attributes in the media library does not update them in the posts automatically.

Luckily, Image Attributes Pro can help you with this and bulk copy image attributes (alt text and image title for now) from the media library to all posts (and products and other post types).

For this, we need to use the filter iaffpro_image_attributes. Copy paste the following code to the functions.php of your current theme.

/**
 * Copy Image Attributes From Media Library to Posts
 * 
 * Check if the attributes are set. If not, generated attributes are used. 
 * 
 * @param refer https://imageattributespro.com/codex/iaffpro_image_attributes/
 * 
 * @author Arun Basil Lal
 * @link https://imageattributespro.com/copy-image-attributes-media-library-to-posts/
 */
function iap_copy_attributes_from_media_library( $attributes, $image_id, $parent_post_id ) {
	
	$attachment = get_post( $image_id );
	
	if ( ! is_object( $attachment ) ) {
		return $attributes;
	}
	
	// Alt Text
	$current_alt_text = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
	if ( strcmp( $current_alt_text, '' ) !== 0 ) {
		$attributes[ 'alt_text' ] = $current_alt_text;
	}
	
	// Image Title
	if ( strcmp( $attachment->post_title, '' ) !== 0 ) {
		$attributes[ 'title' ] = $attachment->post_title;
	}
	
	// Image Caption
	if ( strcmp( $attachment->post_excerpt, '' ) !== 0 ) {
		$attributes[ 'caption' ] = $attachment->post_excerpt;
	}
	
	// Image Description
	if ( strcmp( $attachment->post_content, '' ) !== 0 ) {
		$attributes[ 'description' ] = $attachment->post_content;
	}
	
	return $attributes;
}
add_filter( 'iaffpro_image_attributes', 'iap_copy_attributes_from_media_library', 10, 3 );

What if some posts already have custom Image Title and Alt Text?

Say you updated some of the images in posts with an Image Title and / or an Alt Text when the image was added to the post and you do not want them to be overwritten again.

Image Attributes Pro got you covered. Here is how to do it.

  • In the Bulk Updater Settings tab of the plugin settings page, scroll down to find Image Title Settings and Image Alt Text Settings
  • Each of these settings have a sub option named Bulk Updater Behaviour
  • For Image Title Settings choose: Update image titles in existing posts only if no title is set. All image titles in media library will be updated.
  • For Image Alt Text Settings choose: Update alt text in existing posts only if no alt text is set. All alt text in media library will be updated.

This is how your settings might look like:

Update Title And Alt Text Only When Not Set

Expected output

With the above configuration, once you run the bulk updater, the expected output is as below.

  • No attributes are set in the media library and no attributes are set in the image in the post: Image Attributes Pro will generate attributes as per the settings (i.e. either image filename or post title) and update both the media library and post.
  • Some or all attributes are set in the media library, but no attributes are set in the post: Attributes available in the media library will be copied to the post. Missing attributes in the media library will be updated based on settings.
  • Some or all attributes are set in the media library and some attributes are set in the post: Missing attributes in both the media library and the posts are updated. If the post already has some attributes, they are not overwritten.

Please remember to test the bulk updater using the Test Bulk Updater button to make sure the expected output is what you want.

Sounds good. How do I get started?

Checkout Image Attributes Pro and the pricing plans. If you have any more questions, please do not hesitate to get in touch.