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
5653
5654      $loading_attrs = array();
5655
5656      /*
5657       * Skip lazy-loading for the overall block template, as it is handled more granularly.
5658       * The skip is also applicable for `fetchpriority`.
5659       */
5660      if ( 'template' === $context ) {
5661           /** This filter is documented in wp-includes/media.php */
5662           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5663      }
5664
5665      // For now this function only supports images and iframes.
5666      if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
5667           /** This filter is documented in wp-includes/media.php */
5668           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5669      }
5670
5671      /*
5672       * Skip programmatically created images within content blobs as they need to be handled together with the other
5673       * images within the post content or widget content.
5674       * Without this clause, they would already be considered within their own context which skews the image count and
5675       * can result in the first post content image being lazy-loaded or an image further down the page being marked as a
5676       * high priority.
5677       */
5678      if (
5679           'the_content' !== $context && doing_filter( 'the_content' ) ||
5680           'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
5681           'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
5682      ) {
5683           /** This filter is documented in wp-includes/media.php */
5684           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5685
5686      }
5687
5688      /*
5689       * Add `decoding` with a value of "async" for every image unless it has a
5690       * conflicting `decoding` attribute already present.
5691       */
5692      if ( 'img' === $tag_name ) {
5693           if ( isset( $attr['decoding'] ) ) {
5694                $loading_attrs['decoding'] = $attr['decoding'];
5695           } else {
5696                $loading_attrs['decoding'] = 'async';
5697           }
5698      }
5699
5700      // For any resources, width and height must be provided, to avoid layout shifts.
5701      if ( ! isset( $attr['width'], $attr['height'] ) ) {
5702           /** This filter is documented in wp-includes/media.php */
5703           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5704      }
5705
5706      /*
5707       * The key function logic starts here.
5708       */
5709      $maybe_in_viewport    = null;
5710      $increase_count       = false;
5711      $maybe_increase_count = false;
5712
 
Line Code
5839       * Filters the loading optimization attributes.
5840       *
5841       * @since 6.4.0
5842       *
5843       * @param array  $loading_attrs The loading optimization attributes.
5844       * @param string $tag_name      The tag name.
5845       * @param array  $attr          Array of the attributes for the tag.
5846       * @param string $context       Context for the element for which the loading optimization attribute is requested.
5847       */
5848      return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5849 }
5850
5851 /**
5852  * Gets the threshold for how many of the first content media elements to not lazy-load.
5853  *
5854  * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3.
5855  * The filter is only run once per page load, unless the `$force` parameter is used.
5856  *
5857  * @since 5.9.0