WordPress: Add Image Title in Image Links in WP 3.5

WP 3.5 Image Titles in Link TagsAfter upgrading to WordPress 3.5, there is a subtle change in the way images are being handled in the new media manager. Aside from the improved new "Add Media" interface, multi-image handling capabilities and other features, WordPress also decided to remove the image titles from the output, after adding images to the editor.

In the function wp_ajax_send_attachment_to_editor(), located in wp-admin/includes/ajax-actions.php, a short comment has been added, signifying the change:

if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
$align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
$size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
$alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
$caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
$title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
...
}

Although removing the title attribute from the image tag was a good move, the fact that it is also removed from the <a> link, if placed, is not. As you can see in the function get_image_send_to_editor(), which you can find in wp-admin/includes/media.php, the title, is completely ignored when adding the image to editor:

function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
	$html = get_image_tag($id, $alt,'', $align, $size);
	...
}

Image Title in Links

Having the image title in the link attributes when links are added is important for some features, for example jQuery lightboxes, which rely on the link title attribute to add a title to the lightbox. Although one can manually add a title, this may often cause inconvenience for clients or commercial themes. In the way it's set on 3.5, there's no practical way to output the image titles on the front-end, unless using an image gallery.

Output Image Titles when Adding Images

To add the image titles to a links title attribute, if a link has been specified by the user, will require an short function that we will add to the image_send_to_editor filter:

function send_attachment_with_title($html, $id, $attachment) { 
    //first, we grab the title from the passed $id
    $title = get_the_title($id); 
    //first we check if there is a link with a preg_match
    preg_match("/<a[^>]*>/", $html, $matches);
    // if there is no match, do nothing or add title to image
    if(!isset($matches[0])) {
        $html = str_replace("<img ", "<img title=\"$title\" ", $html);
        return $html;
    }   
    // Then we add it to the link with a str_replace and add it back to $html
    $html = str_replace("<a ", "<a title=\"$title\" ", $html);
    // return $html
    return $html;
}
// Now we have to add this to the image_send_to_editor filter, which runs before it lands on the wp editor
add_filter('image_send_to_editor', 'send_attachment_with_title', 10, 3);

Add this function to your functions.php file in WordPress and that's it. Your inserted images with links should now have the title attribute included in them.

Please feel free to add better suggestions or fixes in the code.

I'm a developer at Maximus Business.
  • Jack
    Great addition, or should I say, reinstall of something I was using always. Because I'm using the jquery colorbox plugin I wanted the titles to show beneath the popup. After upgrading to 3.5 they were gone and I had to manually add the title with each and every image. Thanks very much for this code! A real Christmas present ;). And here's one for you (although in Dutch): http://www.jacktummers.nl/blog/kerst-en-nieuwjaarswens

    Said on Dec 25, 2012 by Jack - Reply
    • Maximilian Ruthe
      Geen Probleem, I'm glad you like the post - I too use colorbox and thought this would help. Ook hartelijk dank voor de gift en Vrolijk Kerstfeest.

      Said on Dec 25, 2012 by Maximilian Ruthe - Reply
  • Bob
    I've been looking around for a way to add the title field back to all images, and this is the closest a solution yet! I tried just changing the <a to <img in the function above, but that doesn't seem to be working, any chance you have the time to rework the function so it works for images instead of links, or even better, for both?

    Said on Jan 13, 2013 by Bob - Reply
    • Maximilian
      Sorry for the late reply on this. If you or anyone else still needs the title in the image, I went ahead and added it to the above code after the if(!isset...): $html = str_replace("<img ", "<img title=\"$title\" ", $html); Now, if no link is found, it will add the title to the image instead. If you want it to always add the title you may remove the preg_match and leave the other rules after the first return $html, since the function ends after a return is triggered anyway. Hope that helps someone.

      Said on Jan 17, 2013 by Maximilian - Reply
  • Samuli Viitasaari
    Thanks for this neat fix! Now I get to use Swipebox again with WP 3.5+ I wonder if you could look into how to do the same for images inserted using standard Wordpress gallery functionality? This code doesn't seem to add titles to those links, although it works fine with individual images...

    Said on Oct 28, 2013 by Samuli Viitasaari - Reply
    • Miranda
      We've been discussing this very thing at length. :P See here: http://wordpress.org/support/topic/no-lightbox-caption-displaying?replies=4 Basically, the working solution I've found for adding titles back to gallery images (their links actually, which is what my plugin uses for captions) is this: function my_get_attachment_link_filter( $content,$id ) { $title = get_the_title($id); $new_content = str_replace('&lt;a &#039;, &#039;&lt;a title=&quot;&#039; . esc_attr($title) . &#039;&quot; &#039;, $content); return $new_content; } add_filter(&#039;wp_get_attachment_link&#039;, &#039;my_get_attachment_link_filter&#039;, 10, 4); Max, do you know if the recent WP update broke the part of your snippet for adding the title back to the ? I found this post and tried it out a few weeks ago and it worked perfectly, but now I'm seeing the title added to the img only.

      Said on Nov 9, 2013 by Miranda - Reply

Leave a Reply

Please feel free to give us your feedback and comment below. Please keep in mind that comments are moderated. Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>