wppaste
WordPress

wp_iframe_tag_add_loading_attr( string $iframe, string $context ): string

Since
5.7.0
Source
wp-includes/media.php:2457
Adds 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 iframe tag where the attribute should be added.
$contextstring
Additional context to pass to the filters.

Return value

string
Converted iframe tag with loading attribute 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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
20–48

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

Plugin surface
1 hook

Third-party callbacks on 'wp_iframe_tag_add_loading_attr' 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

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.

WhenInstructionsCalls it makes
!$iframe20–26width()
$iframe35–38width(), apply_filters()
$iframe44–48width(), apply_filters(), esc_attr()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5220–488
8.55220–488
8.45220–48818 fewer instructions than PHP 8.3
8.37029–668
8.27029–668
8.17029–668
7.47029–668

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:

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

Used by · 1

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.

  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.

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.