wppaste
WordPress

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

Since
6.3.0, 7.0.0
Source
wp-includes/media.php:6461
Determines whether to add fetchpriority='high' to loading attributes.

Compatibility

WordPress
since 7.0.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

$loading_attrs
$tag_namestring
The tag name.
$attr

Return value

array<string,
string> Updated loading optimization attributes for the element.

Performance profile

How much work a call to wp_maybe_add_fetchpriority_high_attr() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–39

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

Plugin surface
1 hook

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

Called by
1

1 place 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

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
always6–19none
$tag_name === "img"16–22wp_high_priority_element_flag()
$tag_name === "img" && wp_high_priority_element_flag() && !$wp_min_priority_img_pixels26–32wp_high_priority_element_flag(), apply_filters()
$tag_name === "img" && wp_high_priority_element_flag() && $wp_min_priority_img_pixels31–39wp_high_priority_element_flag(), apply_filters(), wp_high_priority_element_flag()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 50 instructions, 6–39 executed per call, 10 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 · 1

One hook fires while wp_maybe_add_fetchpriority_high_attr() runs, in this order:

  1. apply_filters( wp_min_priority_img_pixels )filterline 6501 (+40 into the body)

    Filters the minimum square-pixels threshold for an image to be eligible as the high-priority image.

Uses · 2

Used by · 1

Source code

function wp_maybe_add_fetchpriority_high_attr( $loading_attrs, $tag_name, $attr ) {	// For now, adding `fetchpriority="high"` is only supported for images.	if ( 'img' !== $tag_name ) {		return $loading_attrs;	} 	$existing_fetchpriority = $attr['fetchpriority'] ?? null;	if ( null !== $existing_fetchpriority && 'auto' !== $existing_fetchpriority ) {		/*		 * When an IMG has been explicitly marked with `fetchpriority=high`, then honor that this is the element that		 * should have the priority. In contrast, the Navigation block may add `fetchpriority=low` to an IMG which		 * appears in the Navigation Overlay; such images should never be considered candidates for		 * `fetchpriority=high`. Lastly, block visibility may add `fetchpriority=auto` to an IMG when the block is		 * conditionally displayed based on viewport size. Such an image is considered an LCP element candidate if it		 * exceeds the threshold for the minimum number of square pixels.		 */		if ( 'high' === $existing_fetchpriority ) {			$loading_attrs['fetchpriority'] = 'high';			wp_high_priority_element_flag( false );		} 		return $loading_attrs;	} 	// Lazy-loading and `fetchpriority="high"` are mutually exclusive.	if ( isset( $loading_attrs['loading'] ) && 'lazy' === $loading_attrs['loading'] ) {		return $loading_attrs;	} 	if ( ! wp_high_priority_element_flag() ) {		return $loading_attrs;	} 	/**	 * Filters the minimum square-pixels threshold for an image to be eligible as the high-priority image.	 *	 * @since 6.3.0	 *	 * @param int $threshold Minimum square-pixels threshold. Default 50000.	 */	$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 ); 	if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {		if ( 'auto' !== $existing_fetchpriority ) {			$loading_attrs['fetchpriority'] = 'high';		}		wp_high_priority_element_flag( false );	} 	return $loading_attrs;}

Changelog

Introduced in 6.3.0. 3 changes between 6.7.7 and 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.

7.0.4
Parameter $loading_attrs retyped from array to untyped.verified against source
7.0.4
Parameter $attr retyped from array to untyped.verified against source
7.0.4
Return type changed from array to array<string,.verified against source
7.0.0
Support is added for IMG tags with fetchpriority='low' and fetchpriority='auto'.from the docblock
6.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/media.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.