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