get_header_image_tag( array $attr = array() ): string
- Since
- 4.4.0
- Source
wp-includes/theme.php:1267
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
- Scaling
- Scales with input
- Instructions
- 11–141
- Plugin surface
- 2 hooks
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 151.
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.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$header | 11 | get_custom_header(), get_header_image() |
$header && empty($header) | 75–87 | get_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–98 | get_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–100 | get_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–111 | get_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–121 | get_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–130 | get_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–132 | get_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–141 | get_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:
- apply_filters( get_header_image_tag_attributes )filterline 1350 (+83 into the body)
Filters the list of header image attributes.
- apply_filters( get_header_image_tag )filterline 1370 (+103 into the body)
Filters the markup of header images.
Uses · 9
- get_custom_header()Gets the header image data.
- get_header_image()Retrieves header image for custom header.
- absint()Converts a value to non-negative integer.
- get_post_meta()Retrieves a post meta field for the given post ID.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_calculate_image_srcset()A helper function to calculate the image sources to include in a 'srcset' attribute.
- wp_calculate_image_sizes()Creates a 'sizes' attribute value for an image.
- wp_get_loading_optimization_attributes()Gets loading optimization attributes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 2
- get_custom_header_markup()Retrieves the markup for a custom header.
- the_header_image_tag()Displays the image markup for a custom header image.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.