wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_get_loading_attr_default › WordPress Function
Since5.9.0
Deprecatedn/a
› wp_get_loading_attr_default ( $context )
Parameters: |
|
Returns: |
|
Defined at: |
|
Codex: |
Gets the default value to use for a `loading` attribute on an element.
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 booleanfalse
, 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 {@see} 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
{@see 'wp_omit_loading_attr_threshold'} filter.Source
function wp_get_loading_attr_default( $context ) { // 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. $header_area = WP_TEMPLATE_PART_AREA_HEADER; if ( "template_part_{$header_area}" === $context ) { 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'; }