WP hooks navigation: Home/browse • Actions index • Filters index
To save our bandwidth, we show only a snippet of code around each occurence of the hook. View complete file in SVN (without highlighting).
The best way to understand what a hook does is to look at where it occurs in the source code.
do_action( "hook_name" )apply_filters( "hook_name", "what_to_filter" ).Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.
This hook occurs 5 times in this file.
| Line | Code |
|---|---|
| 6001 | |
| 6002 | $loading_attrs = array(); |
| 6003 | |
| 6004 | /* |
| 6005 | * Skip lazy-loading for the overall block template, as it is handled more granularly. |
| 6006 | * The skip is also applicable for `fetchpriority`. |
| 6007 | */ |
| 6008 | if ( 'template' === $context ) { |
| 6009 | /** This filter is documented in wp-includes/media.php */ |
| 6010 | return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 6011 | } |
| 6012 | |
| 6013 | // For now this function only supports images and iframes. |
| 6014 | if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) { |
| 6015 | /** This filter is documented in wp-includes/media.php */ |
| 6016 | return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 6017 | } |
| 6018 | |
| 6019 | /* |
| 6020 | * Skip programmatically created images within content blobs as they need to be handled together with the other |
| 6021 | * images within the post content or widget content. |
| 6022 | * Without this clause, they would already be considered within their own context which skews the image count and |
| 6023 | * can result in the first post content image being lazy-loaded or an image further down the page being marked as a |
| 6024 | * high priority. |
| 6025 | */ |
| 6026 | if ( |
| 6027 | 'the_content' !== $context && doing_filter( 'the_content' ) || |
| 6028 | 'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) || |
| 6029 | 'widget_block_content' !== $context && doing_filter( 'widget_block_content' ) |
| 6030 | ) { |
| 6031 | /** This filter is documented in wp-includes/media.php */ |
| 6032 | return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 6033 | |
| 6034 | } |
| 6035 | |
| 6036 | /* |
| 6037 | * Add `decoding` with a value of "async" for every image unless it has a |
| 6038 | * conflicting `decoding` attribute already present. |
| 6039 | */ |
| 6040 | if ( 'img' === $tag_name ) { |
| 6041 | if ( isset( $attr['decoding'] ) ) { |
| 6042 | $loading_attrs['decoding'] = $attr['decoding']; |
| 6043 | } else { |
| 6044 | $loading_attrs['decoding'] = 'async'; |
| 6045 | } |
| 6046 | } |
| 6047 | |
| 6048 | // For any resources, width and height must be provided, to avoid layout shifts. |
| 6049 | if ( ! isset( $attr['width'], $attr['height'] ) ) { |
| 6050 | /** This filter is documented in wp-includes/media.php */ |
| 6051 | return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 6052 | } |
| 6053 | |
| 6054 | /* |
| 6055 | * The key function logic starts here. |
| 6056 | */ |
| 6057 | $maybe_in_viewport = null; |
| 6058 | $increase_count = false; |
| 6059 | $maybe_increase_count = false; |
| 6060 | |
| Line | Code |
| 6187 | * Filters the loading optimization attributes. |
| 6188 | * |
| 6189 | * @since 6.4.0 |
| 6190 | * |
| 6191 | * @param array $loading_attrs The loading optimization attributes. |
| 6192 | * @param string $tag_name The tag name. |
| 6193 | * @param array $attr Array of the attributes for the tag. |
| 6194 | * @param string $context Context for the element for which the loading optimization attribute is requested. |
| 6195 | */ |
| 6196 | return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 6197 | } |
| 6198 | |
| 6199 | /** |
| 6200 | * Gets the threshold for how many of the first content media elements to not lazy-load. |
| 6201 | * |
| 6202 | * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3. |
| 6203 | * The filter is only run once per page load, unless the `$force` parameter is used. |
| 6204 | * |
| 6205 | * @since 5.9.0 |