WordPress.org

Ready to get started?Download WordPress

Forums

Restoring titles to inserted images in WordPress 3.5 (12 posts)

  1. 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: http://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.

  2. boah123
    Member
    Posted 1 year ago #

    Thanks for sharing!

    I was just looking for this.
    I've added your code in my functions.php theme file and with success. TNX!

  3. OllieJones
    Member
    Posted 1 year ago #

    Les Bessant, I'm responding to your request for a code review. This code solves your problem fine. But I see two issues that should be addressed before it goes out for general use.

    First, if it's activated on a pre 3.5 installation it will put an extra title attribute into the image; it inserts the title attribute without first checking whether one is already present.

    Second, if the post_title happens to contain markup characters your code will emit a malformed img tag.

    Other than that, looks good.

  4. Thanks- I'll have another crack at it later.

  5. OK, revised version. This checks for title= in $html before doing anything. I've applied esc_attr() on post_title which should prevent most forms of breakage.

    Had a few issues with the changed code in the actual plugin. Code coming soon.

  6. And here it is, with typos fixed. Confirmed that markup in the image title is now handled.

    <?php
    /*
    Plugin Name: Restore Image Title
    Description: Reverses WP 3.5's behaviour of stripping title from images inserted into posts
    Version: 1.1
    License: GPL
    Author: Les Bessant
    Author URI: http://losingit.me.uk/
    */
    
    function lcb_restore_image_title( $html, $id ) {
    
    	$attachment = get_post($id);
        if (strpos($html, "title=")) {
        	return $html;
        	}
        else {
    		$mytitle = esc_attr($attachment->post_title);
    		return str_replace('<img', '<img title="' . $mytitle . '" '  , $html);
    		}
    }
    add_filter( 'media_send_to_editor', 'lcb_restore_image_title', 15, 2 );
    ?>
  7. This is now available in the plugin directory:

    http://wordpress.org/extend/plugins/restore-image-title/

  8. mssbee
    Member
    Posted 1 year ago #

    Thanks Les,

    I really was surprised that WP 3.5 removed the image titles since it is an important part of SEO. Your plugin is a life saver.

  9. boah123
    Member
    Posted 1 year ago #

    I'm also using the the_post_thumbnail() function. This displays the associated post thumbnail. Here the title tag is also missing.

    To test this you need:
    - add 'add_theme_support('post-thumbnails');' inside functions.php
    - associate image to post
    - use 'the_post_thumbnail();' inside post template

    Can you also add the title inside this image?

  10. grizgza
    Member
    Posted 1 year ago #

    To restore the title attribute to images in a gallery, this worked code worked for me:

    //Add title attribute back to gallery images
    function my_image_titles($atts,$img) {
    	$atts['title'] = trim(strip_tags( $img->post_title ));
    	return $atts;
    }
    add_filter('wp_get_attachment_image_attributes','my_image_titles',10,2);
  11. boah123
    Member
    Posted 1 year ago #

    @grizgza, thank you very much!

    @Les Bessant, maybe this is a nice add-on for your plugin.

  12. conualfy
    Member
    Posted 1 year ago #

    Thanks @Les Bessant, it works.

    But I think the solution might be a bit different: as we have the title for the <a> tag, but not for the image, maybe the TITLE for <a> should be changed. the current form gets us 2 titles: one for <a> (=page/post title) and one for <img> (=image title).

Topic Closed

This topic has been closed to new replies.

About this Topic