wp_iframe_tag_add_loading_attr( string $iframe, string $context ): string
- Since
- 5.7.0
- Source
wp-includes/media.php:2457
loading attribute to an iframe HTML tag.Compatibility
- WordPress
- since 5.7.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
$iframestring- The HTML
iframetag where the attribute should be added. $contextstring- Additional context to pass to the filters.
Return value
string- Converted
iframetag withloadingattribute added.
Performance profile
How much work a call to wp_iframe_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
- 20–48
- 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 52.
Third-party callbacks on 'wp_iframe_tag_add_loading_attr' 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
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_iframe_tag_add_loading_attr() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$iframe | 20–26 | width() |
$iframe | 35–38 | width(), apply_filters() |
$iframe | 44–48 | width(), apply_filters(), esc_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 52 | 20–48 | 8 | |
| 8.5 | 52 | 20–48 | 8 | |
| 8.4 | 52 | 20–48 | 8 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 70 | 29–66 | 8 | |
| 8.2 | 70 | 29–66 | 8 | |
| 8.1 | 70 | 29–66 | 8 | |
| 7.4 | 70 | 29–66 | 8 |
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_iframe_tag_add_loading_attr() runs, in this order:
- apply_filters( wp_iframe_tag_add_loading_attr )filterline 2499 (+42 into the body)
Filters the `loading` attribute value to add to an iframe. Default 'lazy'.
Uses · 4
- wp_get_loading_optimization_attributes()Gets loading optimization attributes.
- 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.
Used by · 1
- wp_filter_content_tags()Filters specific tags in post content and modifies their markup.
Source code
function wp_iframe_tag_add_loading_attr( $iframe, $context ) { /* * Get loading attribute value to use. This must occur before the conditional check below so that even iframes that * are ineligible for being lazy-loaded are considered. */ $optimization_attrs = wp_get_loading_optimization_attributes( 'iframe', array( /* * The concrete values for width and height are not important here for now * since fetchpriority is not yet supported for iframes. * TODO: Use WP_HTML_Tag_Processor to extract actual values once support is * added. */ 'width' => str_contains( $iframe, ' width="' ) ? 100 : null, 'height' => str_contains( $iframe, ' height="' ) ? 100 : null, // This function is never called when a 'loading' attribute is already present. 'loading' => null, ), $context ); // Iframes should have source and dimension attributes for the `loading` attribute to be added. if ( ! str_contains( $iframe, ' src="' ) || ! str_contains( $iframe, ' width="' ) || ! str_contains( $iframe, ' height="' ) ) { return $iframe; } $value = $optimization_attrs['loading'] ?? false; /** * Filters the `loading` attribute value to add to an iframe. Default 'lazy'. * * Returning `false` or an empty string will not add the attribute. * Returning `true` will add the default value. * * @since 5.7.0 * * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in * the attribute being omitted for the iframe. * @param string $iframe The HTML `iframe` tag to be filtered. * @param string $context Additional context about how the function was called or where the iframe tag is. */ $value = apply_filters( 'wp_iframe_tag_add_loading_attr', $value, $iframe, $context ); if ( $value ) { if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { $value = 'lazy'; } return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe ); } return $iframe;}Changelog
Introduced in 5.7.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/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.