Not sure if this’ll work for you because I don’t know if $attachment_ID
in the function is returning anything at all… …but assuming it is:
function custom_pic_alt( $attachment_ID ) {
//var_dump( $attachment_ID ); //uncomment this and see if it even returns anything.
$thumb_alt = get_post_meta( $attachment_ID, '_wp_attachment_image_alt', true );
$thumb_caption = wp_get_attachment_caption( $attachment_ID );
$thumb_title = get_the_title( $attachment_ID );
if( !empty( $thumb_alt ) ) :
$alt_text = $thumb_alt;
elseif( empty( $thumb_alt) && !empty( $thumb_caption ) ) :
$alt_text = $thumb_caption;
elseif( empty( $thumb_alt ) && empty( $thumb_caption ) && !empty( $thumb_title ) ) :
$alt_text = $thumb_title;
else :
$alt_text="";
endif;
// Return ALT
return esc_attr( trim( strip_tags( $alt_text ) ) );
}
You just get all three values and then run conditional checks to see if they’re empty or not.