WordPress at a glance

get_header_image_tag() WP 1.0

Create image tag markup for a custom header image.

Is the basis for: get_custom_header_markup()
Hooks from function:
Return

String. HTML image element markup or empty string on failure.

Usage

get_header_image_tag( $attr );
$attr(array)
Additional attributes for the image tag. Can be used to override the default attributes.
Default: ''

Code of get_header_image_tag: wp-includes/theme.php VER 4.9.8

<?php
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 );

	$attr = wp_parse_args(
		$attr,
		array(
			'src' => $header->url,
			'width' => $width,
			'height' => $height,
			'alt' => get_bloginfo( 'name' ),
		)
	);

	// 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 );
			$sizes = ! empty( $attr['sizes'] ) ? $attr['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_map( 'esc_attr', $attr );
	$html = '<img';

	foreach ( $attr as $name => $value ) {
		$html .= ' ' . $name . '="' . $value . '"';
	}

	$html .= ' />';

	/**
	 * Filters the markup of header images.
	 *
	 * @since 4.4.0
	 *
	 * @param string $html   The HTML image tag markup being filtered.
	 * @param object $header The custom header object returned by 'get_custom_header()'.
	 * @param array  $attr   Array of the attributes for the image tag.
	 */
	return apply_filters( 'get_header_image_tag', $html, $header, $attr );
}

Related Functions

From tag: custom_header (theme options: video color image)

More from category: Other Theme Functions

No comments
    Hello, !     Log In . Register