Quantcast
Channel: kristarella.blog
Viewing all articles
Browse latest Browse all 10

Add EXIF to WordPress gallery image titles

$
0
0
Image title containing EXIF

Image title containing EXIF

My Exifography plugin provides different ways for users to display EXIF data for their images in WordPress.

One of the ways people like to show EXIF is within the image title, like Sam does on daily dose of imagery. This allows the EXIF to show up in a tooltip when hovering over the image, or if you use a lightbox plugin it might include that information in the popup.

If you have a single image, or a small number of large images, on the page you can just add the Exifography shortcode within title="" to include the EXIF in the image’s title attribute. However, if you’re using the WordPress gallery shortcode, the title attribute is rendered automatically for you. Thankfully, all the attributes that are output with an image in WordPress can be filtered.

The following PHP in your theme’s functions.php, or custom_functions.php in Thesis, will add the EXIF data after the image name in the image’s title attribute (when Exifography is active).

function my_image_titles($atts,$img) {
	if (function_exists('exifography_display_exif'))
		$atts['title'] = trim(strip_tags( $img->post_title )) .' '. exifography_display_exif('all',$img->ID);
	else
		$atts['title'] = trim(strip_tags( $img->post_title ));
	
	return $atts;
}
add_filter('wp_get_attachment_image_attributes','my_image_titles',10,2);

Make sure that your Exifography display settings are suitable for the title attribute, which doesn’t render HTML; you will need to use plain text separators to format the EXIF data. Like the example below.

Image title containing EXIF

If you use a lightbox that uses both the title and alt attributes in the popup, you can modify the code to suit that. prettyPhoto, for example, uses the alt attribute as the image name and it uses the title attribute as the description. In that case you would want to remove trim(strip_tags( $img->post_title )) .' '. from before the Exifography function as to not repeat the image name.

WordPress uses the image name as the alt attribute by default if there’s not a different alt attribute set, so you shouldn’t need to worry about it, but if you do want to change it you can filter it in the same way the title is filtered using $atts['alt'] instead of $atts['title'].


Viewing all articles
Browse latest Browse all 10

Trending Articles