How to Add Title Attributes to Images in WordPress

Place this code into your functions.php file.

function inserted_image_titles( $html, $id ) {
	$attachment = get_post($id);
	$thetitle = $attachment->post_title;
	$thedescription = $attachment->post_description;
	return str_replace('<img', '<img title="' . $thetitle . '" ' .'description="' . $thedescription . '" '  , $html);
}
add_filter( 'media_send_to_editor', 'inserted_image_titles', 15, 2 );
function featured_image_titles($attr, $attachment = null){
	$attr['title'] = get_post($attachment->ID)->post_title;
	return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'featured_image_titles', 10, 2);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments