Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Last active October 2, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ControlledChaos/97f6bb360de47a828e7e to your computer and use it in GitHub Desktop.
Save ControlledChaos/97f6bb360de47a828e7e to your computer and use it in GitHub Desktop.
Restore the title attribute in WordPress images.

Restore Image Title Tag

WordPress Snippet

See this gist to restore the title attribute in galleries.

<?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 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment