It has been intriguing me why someone would want to add the
title attribute to an
<img>, so I did a bit of research.
First, I want to apologize for my previous comments which dismissed your wanting to use the
title attribute and saying it is not possible to add it to anything other than links. Somehow in my 15 years of coding HTML, it has completely escaped me that this is an attribute you can add to all visible HTML tags.
Still though, I was wondering what the best approach would be for SEO purposes. Should the
title attribute be added to the
<img> tag, the
<a> tag or both?
If you're wanting your images to come up in Google search results, then use it on your
<img> tags. If you want to improve your site's ranking in Google, don't bother with it on
<img> tags, Google tends to focus on the information in the ALT text. Still though, nice to use it on
<a> tags to get the friendly tooltip. This you can do with Drew's code above.
If you still want to apply the
title attribute to your
<img> tags for image searches, try the code below based on Drew's solution. Please note that I've not tested this myself.
Change line 1860 in
$codeOut .= '<img class="main" src="' . htmlspecialchars($src) . '"';
$codeOut .= '<img class="main" src="' . htmlspecialchars($src) . '"' title="' . htmlspecialchars($title) . '"';