Skip to content

Instantly share code, notes, and snippets.

@thisbit
Created February 4, 2021 08:51
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 thisbit/a8f97e0035d56a85cff7bda023af6044 to your computer and use it in GitHub Desktop.
Save thisbit/a8f97e0035d56a85cff7bda023af6044 to your computer and use it in GitHub Desktop.
<?php
function myprefix_get_attachment_alt( $attachment_ID ) {
// Get ALT
$thumb_alt = get_post_meta( $attachment_ID, '_wp_attachment_image_alt', true );
// No ALT supplied get attachment info
if ( empty( $thumb_alt ) )
$attachment = get_post( $attachment_ID );
// Use caption if no ALT supplied
if ( empty( $thumb_alt ) )
$thumb_alt = $attachment->post_excerpt;
// Use title if no caption supplied either
if ( empty( $thumb_alt ) )
$thumb_alt = $attachment->post_title;
// Return ALT
return esc_attr( trim( strip_tags( $thumb_alt ) ) );
}
// possible way of using
?>
<img src="..." alt="<?php echo myprefix_get_attachment_alt( 10 ); ?>">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment