| | 997 | * Create image markup for a custom header image. |
| | 998 | * |
| | 999 | * @since 4.4.0 |
| | 1000 | * |
| | 1001 | * @param array $attr Optional. Attributes for the image markup. Default empty. |
| | 1002 | * @return string HTML element or empty string on failure. |
| | 1003 | */ |
| | 1004 | function get_header_image_tag( $attr = array() ) { |
| | 1005 | $header = get_custom_header(); |
| | 1006 | |
| | 1007 | if ( empty( $header->attachment_id ) || empty( $header->url ) ) { |
| | 1008 | return ''; |
| | 1009 | } |
| | 1010 | |
| | 1011 | $width = absint( $header->width ); |
| | 1012 | $height = absint( $header->height ); |
| | 1013 | |
| | 1014 | $defaults = array( |
| | 1015 | 'src' => $header->url, |
| | 1016 | 'width' => $width, |
| | 1017 | 'height' => $height, |
| | 1018 | ); |
| | 1019 | |
| | 1020 | $size_array = array( $width, $height ); |
| | 1021 | $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true ); |
| | 1022 | $srcset = wp_calculate_image_srcset( $header->url, $size_array, $image_meta, $header->attachment_id ); |
| | 1023 | $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $header->attachment_id, $header->url ); |
| | 1024 | |
| | 1025 | if ( $srcset && $sizes ) { |
| | 1026 | $defaults['srcset'] = $srcset; |
| | 1027 | $defaults['sizes'] = $sizes; |
| | 1028 | } |
| | 1029 | |
| | 1030 | $attr = wp_parse_args( $attr, $defaults ); |
| | 1031 | $attr = array_map( 'esc_attr', $attr ); |
| | 1032 | $html = '<img'; |
| | 1033 | |
| | 1034 | foreach ( $attr as $name => $value ) { |
| | 1035 | $html .= ' ' . $name . '="' . $value . '"'; |
| | 1036 | } |
| | 1037 | |
| | 1038 | $html .= ' />'; |
| | 1039 | |
| | 1040 | /** |
| | 1041 | * Filter the markup of header images. |
| | 1042 | * |
| | 1043 | * @since 4.4.0 |
| | 1044 | * |
| | 1045 | * @param string $html The HTML markup being filtered. |
| | 1046 | * @param object $header The custom header object returned by 'get_custom_header()' |
| | 1047 | * @param array $attr An array of attributes for the image markup. |
| | 1048 | */ |
| | 1049 | return apply_filters( 'get_header_image_tag', $html, $header, $attr ); |
| | 1050 | } |
| | 1051 | |
| | 1052 | /** |
| | 1053 | * Display the image markup for a custom header image. |
| | 1054 | * |
| | 1055 | * @since 4.4.0 |
| | 1056 | * |
| | 1057 | * @param array $attr Optional. Attributes for the image markup. Default empty. |
| | 1058 | */ |
| | 1059 | function the_header_image_tag( $attr = array() ) { |
| | 1060 | echo get_header_image_tag( $attr ); |
| | 1061 | } |
| | 1062 | |
| | 1063 | /** |