Title: wp_get_loading_attr_default
Published: February 3, 2022
Last modified: August 20, 2026

---

# wp_get_loading_attr_default( string $context ): string|bool

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#wp--skip-link--target)

This function has been deprecated since 6.3.0. Use [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/)
instead.

Gets the default value to use for a `loading` attribute on an element.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#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()](https://developer.wordpress.org/reference/functions/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’](https://developer.wordpress.org/reference/hooks/wp_omit_loading_attr_threshold/)
filter.

### 󠀁[See also](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#see-also)󠁿

 * [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#parameters)󠁿

 `$context`stringrequired

Context for the element for which the `loading` attribute value is requested.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#source)󠁿

    ```php
    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';
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/deprecated.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/deprecated.php#L4703)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/deprecated.php#L4703-L4772)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_increase_content_media_count()](https://developer.wordpress.org/reference/functions/wp_increase_content_media_count/)`wp-includes/media.php` |

Increases an internal content media count variable.

  | 
| [wp_omit_loading_attr_threshold()](https://developer.wordpress.org/reference/functions/wp_omit_loading_attr_threshold/)`wp-includes/media.php` |

Gets the threshold for how many of the first content media elements to not lazy-load.

  | 
| [WP_Query::is_main_query()](https://developer.wordpress.org/reference/classes/wp_query/is_main_query/)`wp-includes/class-wp-query.php` |

Determines whether the query is the main query.

  | 
| [in_the_loop()](https://developer.wordpress.org/reference/functions/in_the_loop/)`wp-includes/query.php` |

Determines whether the caller is in the Loop.

  | 
| [is_main_query()](https://developer.wordpress.org/reference/functions/is_main_query/)`wp-includes/query.php` |

Determines whether the query is the main query.

  | 
| [doing_filter()](https://developer.wordpress.org/reference/functions/doing_filter/)`wp-includes/plugin.php` |

Returns whether or not a filter hook is currently being processed.

  | 
| [did_action()](https://developer.wordpress.org/reference/functions/did_action/)`wp-includes/plugin.php` |

Retrieves the number of times an action has been fired during the current request.

  | 
| [is_admin()](https://developer.wordpress.org/reference/functions/is_admin/)`wp-includes/load.php` |

Determines whether the current request is for an administrative interface page.

  | 
| [_deprecated_function()](https://developer.wordpress.org/reference/functions/_deprecated_function/)`wp-includes/functions.php` |

Marks a function as deprecated and inform when it has been used.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#)

| Used by | Description | 
| [wp_img_tag_add_loading_attr()](https://developer.wordpress.org/reference/functions/wp_img_tag_add_loading_attr/)`wp-includes/deprecated.php` |

Adds `loading` attribute to an `img` HTML tag.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.3.0](https://developer.wordpress.org/reference/since/6.3.0/) | Deprecated. Use [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/) instead. | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_loading_attr_default%2F)
before being able to contribute a note or feedback.