wp_img_tag_add_loading_attr( string $image, string $context ): string
- Since
- 5.5.0
- Source
wp-includes/deprecated.php:4785
loading attribute to an img HTML tag.Compatibility
Deprecated in WordPress 6.3.0. Use wp_img_tag_add_loading_optimization_attrs() instead.
- WordPress
- since 5.5.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
$imagestring- The HTML
imgtag where the attribute should be added. $contextstring- Additional context to pass to the filters.
Return value
string- Converted
imgtag withloadingattribute added.
Performance profile
How much work a call to wp_img_tag_add_loading_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
- 14–36
- Plugin surface
- 1 hook
- Called by
- 0
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 38.
Third-party callbacks on 'wp_img_tag_add_loading_attr' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_img_tag_add_loading_attr() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$image | 14–18 | _deprecated_function(), wp_get_loading_attr_default() |
$image | 26 | _deprecated_function(), wp_get_loading_attr_default(), apply_filters() |
$image | 35–36 | _deprecated_function(), wp_get_loading_attr_default(), apply_filters(), esc_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 38 | 14–36 | 5 | |
| 8.5 | 38 | 14–36 | 5 | |
| 8.4 | 38 | 14–36 | 5 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 50 | 17–48 | 5 | |
| 8.2 | 50 | 17–48 | 5 | |
| 8.1 | 50 | 17–48 | 5 | |
| 7.4 | 50 | 17–48 | 5 |
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_img_tag_add_loading_attr() runs, in this order:
- apply_filters( wp_img_tag_add_loading_attr )filterline 4799 (+14 into the body)
Filters the `loading` attribute value to add to an image. Default 'lazy'.
Uses · 5
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- wp_get_loading_attr_default()Gets the default value to use for a `loading` attribute on an element.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_attr()Escaping for HTML attributes.
Source code
function wp_img_tag_add_loading_attr( $image, $context ) { _deprecated_function( __FUNCTION__, '6.3.0', 'wp_img_tag_add_loading_optimization_attrs()' ); /* * Get loading attribute value to use. This must occur before the conditional check below so that even images that * are ineligible for being lazy-loaded are considered. */ $value = wp_get_loading_attr_default( $context ); // Images should have source and dimension attributes for the `loading` attribute to be added. if ( ! str_contains( $image, ' src="' ) || ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) { return $image; } /** This filter is documented in wp-includes/media.php */ $value = apply_filters( 'wp_img_tag_add_loading_attr', $value, $image, $context ); if ( $value ) { if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { $value = 'lazy'; } return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image ); } return $image;}Changelog
Introduced in 5.5.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/deprecated.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.