WordPress: Add Title to IMG tag using Get The Image plugin

This image is a screenshot from Get The Image plugin.
This image is a screenshot from Get The Image plugin.

Get The Image plugin from Justin Tadlock is awesome, a script that can grab an image by custom field input, WordPress’ featured image, post attachment, or extracting it from the post’s content.

Recently, someone asked about how to add Title to IMG tag. We know ALT attributes in IMG tag, but IMG tag also support Title attributes as well. These two attributes supposed to describe the image in words and impact image search rankings. ALT attribute should describe what the image is about, while Title should follow the rules of the regular title.

Get The Image only have ALT attribute, we are adding Title attribute by changing following code
1. Open get-the-image.php with Text Editor. I’m using Text Wrangler on Mac
2. Find following code, it’s in line 459-460 on Text Wrangler. Change it from

/* Add the image attributes to the <img /> element. */
	$html = '<img src="' . $image['src'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';

to

/* Add the image attributes to the <img /> element. */
    $html = '<img src="' . $image['src'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';

Comments

Leave a Reply

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

%d bloggers like this: