wp_maybe_add_fetchpriority_high_attr( array $loading_attrs, string $tag_name, array $attr ): array

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Determines whether to add fetchpriority='high' to loading attributes.

Parameters

$loading_attrsarrayrequired
Array of the loading optimization attributes for the element.
$tag_namestringrequired
The tag name.
$attrarrayrequired
Array of the attributes for the element.

Return

array Updated loading optimization attributes for the element.

Source

 * can result in the first post content image being lazy-loaded or an image further down the page being marked as a
 * high priority.
 */
if (
	'the_content' !== $context && doing_filter( 'the_content' ) ||
	'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
	'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
) {
	/** This filter is documented in wp-includes/media.php */
	return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );

}

/*
 * Add `decoding` with a value of "async" for every image unless it has a
 * conflicting `decoding` attribute already present.
 */
if ( 'img' === $tag_name ) {
	if ( isset( $attr['decoding'] ) ) {
		$loading_attrs['decoding'] = $attr['decoding'];
	} else {
		$loading_attrs['decoding'] = 'async';
	}
}

// For any resources, width and height must be provided, to avoid layout shifts.
if ( ! isset( $attr['width'], $attr['height'] ) ) {
	/** This filter is documented in wp-includes/media.php */
	return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
}

/*
 * The key function logic starts here.
 */
$maybe_in_viewport    = null;
$increase_count       = false;
$maybe_increase_count = false;

// Logic to handle a `loading` attribute that is already provided.
if ( isset( $attr['loading'] ) ) {
	/*
	 * Interpret "lazy" as not in viewport. Any other value can be
	 * interpreted as in viewport (realistically only "eager" or `false`
	 * to force-omit the attribute are other potential values).
	 */
	if ( 'lazy' === $attr['loading'] ) {

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.