Skip to content

Instantly share code, notes, and snippets.

@ashleycam3ron
Created February 17, 2020 22:13
Show Gist options
  • Save ashleycam3ron/c76a35f787cb36d0af87bfe92dcb8dfb to your computer and use it in GitHub Desktop.
Save ashleycam3ron/c76a35f787cb36d0af87bfe92dcb8dfb to your computer and use it in GitHub Desktop.
Add Title and Alt Attribute to WordPress Image the_post_thumbnail
<?php /* Add Title and Alt Attribute to WordPress Image the_post_thumbnail */
function ac_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
// or get the title instead of image title $attr['title'] = the_title_attribute( 'echo=0' );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','ac_add_img_title', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment