wp_get_loading_attr_default( string $context ): string|bool
- Since
- 5.9.0
- Source
wp-includes/deprecated.php:4704
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
loadingattribute value is requested.
Return value
string|bool- The default
loadingattribute value. Either 'lazy', 'eager', or a booleanfalse, to indicate that theloadingattribute 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
- Scaling
- Constant
- Instructions
- 10–55
- Plugin surface
- None
- 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 62.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–22 | _deprecated_function() |
$context !== "template" && $context !== false | 20–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
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- doing_filter()Returns whether or not a filter hook is currently being processed.
- did_action()Retrieves the number of times an action has been fired during the current request.
- is_admin()Determines whether the current request is for an administrative interface page.
- in_the_loop()Determines whether the caller is in the Loop.
- is_main_query()Determines whether the query is the main query.
- wp_increase_content_media_count()Increases an internal content media count variable.
- wp_omit_loading_attr_threshold()Gets the threshold for how many of the first content media elements to not lazy-load.
Used by · 1
- wp_img_tag_add_loading_attr()Adds `loading` attribute to an `img` HTML tag.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.