| | 997 | * Create image tag 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->url ) ) { |
| | 1008 | return ''; |
| | 1009 | } |
| | 1010 | |
| | 1011 | $width = absint( $header->width ); |
| | 1012 | $height = absint( $header->height ); |
| | 1013 | |
| | 1014 | $attr = wp_parse_args( |
| | 1015 | $attr, |
| | 1016 | array( |
| | 1017 | 'src' => $header->url, |
| | 1018 | 'width' => $width, |
| | 1019 | 'height' => $height, |
| | 1020 | 'alt' => get_bloginfo( 'name' ), |
| | 1021 | ) |
| | 1022 | ); |
| | 1023 | |
| | 1024 | // Generate 'srcset' and 'sizes' if not already present. |
| | 1025 | if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) { |
| | 1026 | $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true ); |
| | 1027 | $size_array = array( $width, $height ); |
| | 1028 | |
| | 1029 | if ( is_array( $image_meta ) ) { |
| | 1030 | $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id ); |
| | 1031 | $sizes = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id ); |
| | 1032 | |
| | 1033 | if ( $srcset && $sizes ) { |
| | 1034 | $attr['srcset'] = $srcset; |
| | 1035 | $attr['sizes'] = $sizes; |
| | 1036 | } |
| | 1037 | } |
| | 1038 | } |
| | 1039 | |
| | 1040 | $attr = array_map( 'esc_attr', $attr ); |
| | 1041 | $html = '<img'; |
| | 1042 | |
| | 1043 | foreach ( $attr as $name => $value ) { |
| | 1044 | $html .= ' ' . $name . '="' . $value . '"'; |
| | 1045 | } |
| | 1046 | |
| | 1047 | $html .= ' />'; |
| | 1048 | |
| | 1049 | /** |
| | 1050 | * Filter the markup of header images. |
| | 1051 | * |
| | 1052 | * @since 4.4.0 |
| | 1053 | * |
| | 1054 | * @param string $html The HTML markup being filtered. |
| | 1055 | * @param object $header The custom header object returned by 'get_custom_header()' |
| | 1056 | * @param array $attr An array of attributes for the image markup. |
| | 1057 | */ |
| | 1058 | return apply_filters( 'get_header_image_tag', $html, $header, $attr ); |
| | 1059 | } |
| | 1060 | |
| | 1061 | /** |
| | 1062 | * Display the image markup for a custom header image. |
| | 1063 | * |
| | 1064 | * @since 4.4.0 |
| | 1065 | * |
| | 1066 | * @param array $attr Optional. Attributes for the image markup. Default empty. |
| | 1067 | */ |
| | 1068 | function the_header_image_tag( $attr = array() ) { |
| | 1069 | echo get_header_image_tag( $attr ); |
| | 1070 | } |
| | 1071 | |
| | 1072 | /** |