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?


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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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