Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.