wppaste
WordPress

wp_get_loading_attr_default( string $context ): string|bool

Since
5.9.0
Source
wp-includes/deprecated.php:4703
Gets the default value to use for a loading attribute on an element.

Description

This function should only be called for a tag and context if lazy-loading is generally enabled.

The function usually returns 'lazy', but uses certain heuristics to guess whether the current element is likely to appear above the fold, in which case it returns a boolean false, which will lead to the loading attribute being omitted on the element. The purpose of this refinement is to avoid lazy-loading elements that are within the initial viewport, which can have a negative performance impact.

Under the hood, the function uses wp_increase_content_media_count() every time it is called for an element within the main content. If the element is the very first content element, the loading attribute will be omitted.
This default threshold of 3 content elements to omit the loading attribute for can be customized using the 'wp_omit_loading_attr_threshold' filter.

Compatibility

Deprecated in WordPress 6.3.0. Use wp_get_loading_optimization_attributes() instead.

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

$contextstring
Context for the element for which the loading attribute value is requested.

Return value

string|bool
The default loading attribute value. Either 'lazy', 'eager', or a boolean false, to indicate that the loading attribute should be skipped.

Performance profile

How much work a call to wp_get_loading_attr_default() 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
10–55

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

Plugin surface
None

Nothing here hands control to plugin code.

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 callbacksdo_action()one call below wp_get_loading_attr_default()

Further down the call graph this can also reach query, option, cache, serialize 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 · 25 distinct outcomes

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

WhenInstructionsCalls it makes
always10–22_deprecated_function()
$context !== "template" && $context !== false20–28_deprecated_function(), doing_filter()
$context !== "template" && $context !== false && $context !== "the_post_thumbnail" && $context !== "wp_get_attachment_image" && is_admin()23–25_deprecated_function(), is_admin()
$context !== "template" && $context !== false && $context !== "the_post_thumbnail" && $context !== "wp_get_attachment_image" && !is_admin() && !in_the_loop()26–28_deprecated_function(), is_admin(), in_the_loop()
$context !== "template" && $context !== false && !doing_filter() && !$wp_query && is_admin()27–31_deprecated_function(), doing_filter(), is_admin()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && !->is_main_query() && $context !== "the_content" && $context !== "the_post_thumbnail"29–31_deprecated_function(), doing_filter(), ->is_main_query()
$context !== "template" && $context !== false && $context !== "the_post_thumbnail" && $context !== "wp_get_attachment_image" && !is_admin() && in_the_loop() && !is_main_query()29–31_deprecated_function(), is_admin(), in_the_loop(), is_main_query()
$context !== "template" && $context !== false && !doing_filter() && !$wp_query && !is_admin() && !in_the_loop()30–34_deprecated_function(), doing_filter(), is_admin(), in_the_loop()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && !->is_main_query() && is_admin()30–34_deprecated_function(), doing_filter(), ->is_main_query(), is_admin()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && !did_action() && $context !== "the_content" && $context !== "the_post_thumbnail"33–35_deprecated_function(), doing_filter(), ->is_main_query(), did_action()
$context !== "template" && $context !== false && !doing_filter() && !$wp_query && !is_admin() && in_the_loop() && !is_main_query()33–37_deprecated_function(), doing_filter(), is_admin(), in_the_loop(), is_main_query()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && !->is_main_query() && !is_admin() && !in_the_loop()33–37_deprecated_function(), doing_filter(), ->is_main_query(), is_admin(), in_the_loop()
13 further outcomes, up to 55 instructions
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && did_action()33–39_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), did_action()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && !did_action() && is_admin()34–38_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), is_admin()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && !->is_main_query() && !is_admin() && in_the_loop() && !is_main_query()36–40_deprecated_function(), doing_filter(), ->is_main_query(), is_admin(), in_the_loop(), is_main_query()
$context !== "template" && $context !== false && $context !== "the_post_thumbnail" && $context !== "wp_get_attachment_image" && !is_admin() && in_the_loop() && is_main_query()36–38_deprecated_function(), is_admin(), in_the_loop(), is_main_query(), wp_increase_content_media_count(), wp_omit_loading_attr_threshold()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && !did_action() && !is_admin() && !in_the_loop()37–41_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), is_admin(), in_the_loop()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && did_action() && is_admin()38–42_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), did_action(), is_admin()
$context !== "template" && $context !== false && !doing_filter() && !$wp_query && !is_admin() && in_the_loop() && is_main_query()40–44_deprecated_function(), doing_filter(), is_admin(), in_the_loop(), is_main_query(), wp_increase_content_media_count(), wp_omit_loading_attr_threshold()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && !did_action() && !is_admin() && in_the_loop() && !is_main_query()40–44_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), is_admin(), in_the_loop(), is_main_query()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && did_action() && !is_admin() && !in_the_loop()41–45_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), did_action(), is_admin(), in_the_loop()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && !->is_main_query() && !is_admin() && in_the_loop() && is_main_query()43–47_deprecated_function(), doing_filter(), ->is_main_query(), is_admin(), in_the_loop(), is_main_query(), wp_increase_content_media_count(), wp_omit_loading_attr_threshold()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && did_action() && !is_admin() && in_the_loop() && !is_main_query()44–48_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), did_action(), is_admin(), in_the_loop(), is_main_query()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && !did_action() && !is_admin() && in_the_loop() && is_main_query()47–51_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), is_admin(), in_the_loop(), is_main_query(), wp_increase_content_media_count(), wp_omit_loading_attr_threshold()
$context !== "template" && $context !== false && !doing_filter() && $wp_query && ->is_main_query() && did_action() && !is_admin() && in_the_loop() && is_main_query()51–55_deprecated_function(), doing_filter(), ->is_main_query(), did_action(), did_action(), is_admin(), in_the_loop(), is_main_query(), wp_increase_content_media_count(), wp_omit_loading_attr_threshold()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 62 instructions, 10–55 executed per call, 15 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.

Uses · 8

Used by · 1

Source code

function wp_get_loading_attr_default( $context ) {	_deprecated_function( __FUNCTION__, '6.3.0', 'wp_get_loading_optimization_attributes()' );	global $wp_query; 	// Skip lazy-loading for the overall block template, as it is handled more granularly.	if ( 'template' === $context ) {		return false;	} 	/*	 * Do not lazy-load images in the header block template part, as they are likely above the fold.	 * For classic themes, this is handled in the condition below using the 'get_header' action.	 */	$header_area = WP_TEMPLATE_PART_AREA_HEADER;	if ( "template_part_{$header_area}" === $context ) {		return false;	} 	// Special handling for programmatically created image tags.	if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) {		/*		 * Skip programmatically created images within post content as they need to be handled together with the other		 * images within the post content.		 * Without this clause, they would already be counted below which skews the number and can result in the first		 * post content image being lazy-loaded only because there are images elsewhere in the post content.		 */		if ( doing_filter( 'the_content' ) ) {			return false;		} 		// Conditionally skip lazy-loading on images before the loop.		if (			// Only apply for main query but before the loop.			$wp_query->before_loop && $wp_query->is_main_query()			/*			 * Any image before the loop, but after the header has started should not be lazy-loaded,			 * except when the footer has already started which can happen when the current template			 * does not include any loop.			 */			&& did_action( 'get_header' ) && ! did_action( 'get_footer' )		) {			return false;		}	} 	/*	 * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded,	 * as they are likely above the fold.	 */	if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) {		// Only elements within the main query loop have special handling.		if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {			return 'lazy';		} 		// Increase the counter since this is a main query content element.		$content_media_count = wp_increase_content_media_count(); 		// If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted.		if ( $content_media_count <= wp_omit_loading_attr_threshold() ) {			return false;		} 		// For elements after the threshold, lazy-load them as usual.		return 'lazy';	} 	// Lazy-load by default for any unknown context.	return 'lazy';}

Changelog

Introduced in 5.9.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.

6.3.0
Deprecated. Use wp_get_loading_optimization_attributes() instead.from the docblock
5.9.0
Introduced.from the docblock

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.