wp_get_loading_attr_default( string $context ): string|bool
- Since
- 5.9.0
- Source
wp-includes/deprecated.php:4703
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.
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 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.