Restore Image Title Text

In WordPress 3.5, when images are inserted into posts, the title attribute is not included in the image tag.

The Lightbox plugin I am using requires the title tag to be present on images; besides, I like having a hover tool-tip.

Read More

Is there some way to restore the previous behaviour of including the title attribute?

Related posts

Leave a Reply

1 comment

  1. You can hook into the media_send_to_editor filter and add the title tag to the generated HTML image tag:

    function wpse_78529_restore_image_title( $html, $id ) {
    
        /* retrieve the post object */
        $attachment = get_post( $id );
    
        /* if the title attribute is already present, bail early */
        if ( strpos( $html, 'title=' ) )
            return $html;
    
        /* retrieve the attached image attrbute */
        $image_title = esc_attr( $attachment->post_title );
    
        /* apply the title attribute to the image tag */
        return str_replace( '<img', '<img title="' . $image_title . '" ', $html );      
    }
    add_filter( 'media_send_to_editor', 'wpse_78529_restore_image_title', 15, 2 );