wppaste
WordPress

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

Since
4.4.0
Source
wp-includes/theme.php:1270
Creates image tag markup for a custom header image.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attrarrayoptional
Additional attributes for the image tag. Can be used to override the default attributes. Default empty.Default: array()

Return value

string
HTML image element markup or empty string on failure.

Performance profile

How much work a call to get_header_image_tag() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–141

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 151.

Plugin surface
2 hooks

Third-party callbacks on 'get_header_image_tag_attributes', 'get_header_image_tag' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_header_image_tag() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$header11get_custom_header(), get_header_image()
$header && empty($header)75–87get_custom_header(), get_header_image(), absint(), absint(), src(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && !empty($header)85–98get_custom_header(), get_header_image(), absint(), absint(), get_post_meta(), src(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && empty($attr) && !is_array($image_meta)90–100get_custom_header(), get_header_image(), absint(), absint(), src(), get_post_meta(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && !empty($header) && empty($attr) && !is_array($image_meta)100–111get_custom_header(), get_header_image(), absint(), absint(), get_post_meta(), src(), get_post_meta(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && is_array($image_meta)106–121get_custom_header(), get_header_image(), absint(), absint(), src(), get_post_meta(), wp_calculate_image_srcset(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && empty($attr) && is_array($image_meta)115–130get_custom_header(), get_header_image(), absint(), absint(), src(), get_post_meta(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && !empty($header) && is_array($image_meta)116–132get_custom_header(), get_header_image(), absint(), absint(), get_post_meta(), src(), get_post_meta(), wp_calculate_image_srcset(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()
$header && !empty($header) && empty($attr) && is_array($image_meta)125–141get_custom_header(), get_header_image(), absint(), absint(), get_post_meta(), src(), get_post_meta(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), array_map(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 151 instructions, 11–141 executed per call, 17 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_header_image_tag() runs, in this order:

  1. apply_filters( get_header_image_tag_attributes )filterline 1353 (+83 into the body)

    Filters the list of header image attributes.

  2. apply_filters( get_header_image_tag )filterline 1373 (+103 into the body)

    Filters the markup of header images.

Uses · 9

Used by · 2

Source code

function get_header_image_tag( $attr = array() ) {	$header      = get_custom_header();	$header->url = get_header_image(); 	if ( ! $header->url ) {		return '';	} 	$width  = absint( $header->width );	$height = absint( $header->height );	$alt    = ''; 	// Use alternative text assigned to the image, if available. Otherwise, leave it empty.	if ( ! empty( $header->attachment_id ) ) {		$image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true ); 		if ( is_string( $image_alt ) ) {			$alt = $image_alt;		}	} 	$attr = wp_parse_args(		$attr,		array(			'src'    => $header->url,			'width'  => $width,			'height' => $height,			'alt'    => $alt,		)	); 	// Generate 'srcset' and 'sizes' if not already present.	if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {		$image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );		$size_array = array( $width, $height ); 		if ( is_array( $image_meta ) ) {			$srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id ); 			if ( ! empty( $attr['sizes'] ) ) {				$sizes = $attr['sizes'];			} else {				$sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );			} 			if ( $srcset && $sizes ) {				$attr['srcset'] = $srcset;				$attr['sizes']  = $sizes;			}		}	} 	$attr = array_merge(		$attr,		wp_get_loading_optimization_attributes( 'img', $attr, 'get_header_image_tag' )	); 	/*	 * If the default value of `lazy` for the `loading` attribute is overridden	 * to omit the attribute for this image, ensure it is not included.	 */	if ( isset( $attr['loading'] ) && ! $attr['loading'] ) {		unset( $attr['loading'] );	} 	// If the `fetchpriority` attribute is overridden and set to false or an empty string.	if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) {		unset( $attr['fetchpriority'] );	} 	// If the `decoding` attribute is overridden and set to false or an empty string.	if ( isset( $attr['decoding'] ) && ! $attr['decoding'] ) {		unset( $attr['decoding'] );	} 	/**	 * Filters the list of header image attributes.	 *	 * @since 5.9.0	 *

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.