After 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.
Said on Dec 25, 2012 by Jack -
Said on Dec 25, 2012 by Maximilian Ruthe -
Said on Jan 13, 2013 by Bob -
Said on Jan 17, 2013 by Maximilian -
Said on Oct 28, 2013 by Samuli Viitasaari -
Said on Nov 9, 2013 by Miranda -