ControlledChaos of Controlled Chaos Design
8/9/2016 - 6:45 PM

Restore the title attribute in WordPress images.

Restore the title attribute in WordPress images.

<?php

function ccd_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', 'ccd_restore_image_title', 15, 2 );

function ccd_restore_title_to_gallery( $content, $id ) {
	$thumb_title = get_the_title( $id );
	return str_replace( '<a', '<a title="' . esc_attr( $thumb_title ) . '" ', $content );
}	
add_filter( 'wp_get_attachment_link', 'ccd_restore_title_to_gallery', 10, 4 );

?>

Restore Image Title Tag

WordPress Snippet

See this gist to restore the title attribute in galleries.