Menu Adam R Brown

WP hooks navigation: Home/browseActions indexFilters index

Source View: wp_get_loading_optimization_attributes

To save our bandwidth, we show only a snippet of code around each occurence of the hook. View complete file in SVN (without highlighting).

Understanding Source Code

The best way to understand what a hook does is to look at where it occurs in the source code.

Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.

Source View

This hook occurs 5 times in this file.

Line Code
5899
5900      $loading_attrs = array();
5901
5902      /*
5903       * Skip lazy-loading for the overall block template, as it is handled more granularly.
5904       * The skip is also applicable for `fetchpriority`.
5905       */
5906      if ( 'template' === $context ) {
5907           /** This filter is documented in wp-includes/media.php */
5908           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5909      }
5910
5911      // For now this function only supports images and iframes.
5912      if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
5913           /** This filter is documented in wp-includes/media.php */
5914           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5915      }
5916
5917      /*
5918       * Skip programmatically created images within content blobs as they need to be handled together with the other
5919       * images within the post content or widget content.
5920       * Without this clause, they would already be considered within their own context which skews the image count and
5921       * can result in the first post content image being lazy-loaded or an image further down the page being marked as a
5922       * high priority.
5923       */
5924      if (
5925           'the_content' !== $context && doing_filter( 'the_content' ) ||
5926           'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
5927           'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
5928      ) {
5929           /** This filter is documented in wp-includes/media.php */
5930           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5931
5932      }
5933
5934      /*
5935       * Add `decoding` with a value of "async" for every image unless it has a
5936       * conflicting `decoding` attribute already present.
5937       */
5938      if ( 'img' === $tag_name ) {
5939           if ( isset( $attr['decoding'] ) ) {
5940                $loading_attrs['decoding'] = $attr['decoding'];
5941           } else {
5942                $loading_attrs['decoding'] = 'async';
5943           }
5944      }
5945
5946      // For any resources, width and height must be provided, to avoid layout shifts.
5947      if ( ! isset( $attr['width'], $attr['height'] ) ) {
5948           /** This filter is documented in wp-includes/media.php */
5949           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5950      }
5951
5952      /*
5953       * The key function logic starts here.
5954       */
5955      $maybe_in_viewport    = null;
5956      $increase_count       = false;
5957      $maybe_increase_count = false;
5958
 
Line Code
6085       * Filters the loading optimization attributes.
6086       *
6087       * @since 6.4.0
6088       *
6089       * @param array  $loading_attrs The loading optimization attributes.
6090       * @param string $tag_name      The tag name.
6091       * @param array  $attr          Array of the attributes for the tag.
6092       * @param string $context       Context for the element for which the loading optimization attribute is requested.
6093       */
6094      return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
6095 }
6096
6097 /**
6098  * Gets the threshold for how many of the first content media elements to not lazy-load.
6099  *
6100  * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3.
6101  * The filter is only run once per page load, unless the `$force` parameter is used.
6102  *
6103  * @since 5.9.0