get_header_image_tag

Create image tag markup for a custom header image.

Description

(string) get_header_image_tag( (array) $attr = array() ); 

Returns (string)

HTML image element markup or empty string on failure.

Parameters (1)

0. $attr — Optional. (array) => array()
Additional attributes for the image tag. Can be used to override the default attributes. Default empty.

Usage

  1. if ( !function_exists( 'get_header_image_tag' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4. // Optional. Additional attributes for the image tag. Can be used 
  5. // to override the default attributes. Default empty. 
  6. $attr = array(); 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = get_header_image_tag($attr); 
  10.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/theme.php  
  1. function get_header_image_tag( $attr = array() ) { 
  2. $header = get_custom_header(); 
  3. $header->url = get_header_image(); 
  4.  
  5. if ( ! $header->url ) { 
  6. return ''; 
  7.  
  8. $width = absint( $header->width ); 
  9. $height = absint( $header->height ); 
  10.  
  11. $attr = wp_parse_args
  12. $attr,  
  13. array( 
  14. 'src' => $header->url,  
  15. 'width' => $width,  
  16. 'height' => $height,  
  17. 'alt' => get_bloginfo( 'name' ),  
  18. ); 
  19.  
  20. // Generate 'srcset' and 'sizes' if not already present. 
  21. if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) { 
  22. $image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true ); 
  23. $size_array = array( $width, $height ); 
  24.  
  25. if ( is_array( $image_meta ) ) { 
  26. $srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id ); 
  27. $sizes = ! empty( $attr['sizes'] ) ? $attr['sizes'] : wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id ); 
  28.  
  29. if ( $srcset && $sizes ) { 
  30. $attr['srcset'] = $srcset; 
  31. $attr['sizes'] = $sizes; 
  32.  
  33. $attr = array_map( 'esc_attr', $attr ); 
  34. $html = '<img'; 
  35.  
  36. foreach ( $attr as $name => $value ) { 
  37. $html .= ' ' . $name . '="' . $value . '"'; 
  38.  
  39. $html .= ' />'; 
  40.  
  41. /** 
  42. * Filters the markup of header images. 
  43. * @since 4.4.0 
  44. * @param string $html The HTML image tag markup being filtered. 
  45. * @param object $header The custom header object returned by 'get_custom_header()'. 
  46. * @param array $attr Array of the attributes for the image tag. 
  47. */ 
  48. return apply_filters( 'get_header_image_tag', $html, $header, $attr );