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
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
- Scaling
- Constant
- Instructions
- 6–39
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 50.
Third-party callbacks on 'wp_min_priority_img_pixels' run inside this call, and their cost is not bounded by anything here.
1 place 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
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–19 | none |
$tag_name === "img" | 16–22 | wp_high_priority_element_flag() |
$tag_name === "img" && wp_high_priority_element_flag() && !$wp_min_priority_img_pixels | 26–32 | wp_high_priority_element_flag(), apply_filters() |
$tag_name === "img" && wp_high_priority_element_flag() && $wp_min_priority_img_pixels | 31–39 | wp_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:
- 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
- wp_high_priority_element_flag()Accesses a flag that indicates if an element is a possible candidate for `fetchpriority='high'`.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 1
- wp_get_loading_optimization_attributes()Gets loading optimization attributes.
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.
Signature, return type and hooks compared across 5 parsed releases.
$loading_attrs retyped from array to untyped.verified against source$attr retyped from array to untyped.verified against sourcearray to array<string,.verified against sourcefetchpriority='low' and fetchpriority='auto'.from the docblockAbout 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.