636 | | * @param int $attachment_id Image attachment ID. |
637 | | * @param string $size Optional, default is 'thumbnail'. |
638 | | * @param bool $icon Optional, default is false. Whether it is an icon. |
| 644 | * @param int $attachment_id Attachment ID. |
| 645 | * @param string|array $size Keyword or array [width, height]. Ignored for icons. Optional, default is 'thumbnail'. |
| 646 | * @param bool $icon Use an icon image. Ignored for image attachments. Optional, default is false. |
| 647 | * @param array|string $attr HTML attributes to merge in. Optional. |
652 | | 'src' => $src, |
653 | | 'class' => "attachment-$size", |
654 | | 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first |
655 | | 'title' => trim(strip_tags( $attachment->post_title )), |
656 | | ); |
657 | | if ( empty($default_attr['alt']) ) |
658 | | $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption |
659 | | if ( empty($default_attr['alt']) ) |
660 | | $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title |
| 670 | // Use the image's Alternative Text field if available… |
| 671 | 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ))), |
| 672 | 'class' => "attachment-$size", |
| 673 | 'height' => $height, |
| 674 | 'src' => $src, |
| 675 | 'title' => trim( strip_tags( $attachment->post_title )), |
| 676 | 'width' => $width, |
| 677 | ); |
662 | | $attr = wp_parse_args($attr, $default_attr); |
663 | | $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); |
664 | | $attr = array_map( 'esc_attr', $attr ); |
665 | | $html = rtrim("<img $hwstring"); |
| 679 | if ( empty($default_attr['alt']) ) { |
| 680 | // otherwise Caption… |
| 681 | $default_attr['alt'] = trim( strip_tags( $attachment->post_excerpt )); |
| 682 | } |
| 683 | |
| 684 | if ( empty($default_attr['alt']) ) { |
| 685 | // otherwise Title |
| 686 | $default_attr['alt'] = trim( strip_tags( $attachment->post_title )); |
| 687 | } |
| 688 | |
| 689 | $attr = wp_parse_args( $attr, $default_attr ); |
| 690 | $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $icon ); |
| 691 | |
| 692 | if ( !isset( $attr['width'], $attr['height'] )) { |
| 693 | // If dimensions are missing (null) after applying `wp_get_attachment_image_attributes` filter, restore them. |
| 694 | // Plugins can still empty these with ('width' => '', 'height' => ''). |
| 695 | $attr['width'] = ''; |
| 696 | $attr['height'] = ''; |
| 697 | } |
| 698 | |
| 699 | $html = '<img'; |
| 700 | |