Restoring titles to inserted images in WordPress 3.5
-
A decision was made that WordPress 3.5 would not include title attributes when images are inserted into posts, for reasons explained in this Trac ticket.
This has unwanted side-effects on some lightbox plugins, and anyone who wants or needs to use the title attribute has to edit the image after insertion, and enter the required text. That’s not a big deal for anyone who deals with small numbers of images, but for users dealing with a lot of images, it’s a wee bit annoying.
Anyway, I spent some time working out how to get around this, and I’ve made a small plugin which works for me. It’s only been tested on my own sites, so in the usual way, use at your own risk. Please note that my programming skills are minimal, so there are quite likely better solutions, but this appears to work for the moment.
<?php /* Plugin Name: Restore Image Title Attribute Description: Reverses WP 3.5's behaviour of stripping title from images inserted into posts Version: 1.0 License: GPL Author: Les Bessant Author URI: https://losingit.me.uk/ */ function lcb_restore_image_title( $html, $id ) { $attachment = get_post($id); $mytitle = $attachment->post_title; return str_replace('<img', '<img title="' . $mytitle . '" ' , $html); } add_filter( 'media_send_to_editor', 'lcb_restore_image_title', 15, 2 ); ?>
Feel free to use, modify, alter, adapt as appropriate.
- The topic ‘Restoring titles to inserted images in WordPress 3.5’ is closed to new replies.