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
5981
5982      $loading_attrs = array();
5983
5984      /*
5985       * Skip lazy-loading for the overall block template, as it is handled more granularly.
5986       * The skip is also applicable for `fetchpriority`.
5987       */
5988      if ( 'template' === $context ) {
5989           /** This filter is documented in wp-includes/media.php */
5990           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5991      }
5992
5993      // For now this function only supports images and iframes.
5994      if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
5995           /** This filter is documented in wp-includes/media.php */
5996           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
5997      }
5998
5999      /*
6000       * Skip programmatically created images within content blobs as they need to be handled together with the other
6001       * images within the post content or widget content.
6002       * Without this clause, they would already be considered within their own context which skews the image count and
6003       * can result in the first post content image being lazy-loaded or an image further down the page being marked as a
6004       * high priority.
6005       */
6006      if (
6007           'the_content' !== $context && doing_filter( 'the_content' ) ||
6008           'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
6009           'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
6010      ) {
6011           /** This filter is documented in wp-includes/media.php */
6012           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
6013
6014      }
6015
6016      /*
6017       * Add `decoding` with a value of "async" for every image unless it has a
6018       * conflicting `decoding` attribute already present.
6019       */
6020      if ( 'img' === $tag_name ) {
6021           if ( isset( $attr['decoding'] ) ) {
6022                $loading_attrs['decoding'] = $attr['decoding'];
6023           } else {
6024                $loading_attrs['decoding'] = 'async';
6025           }
6026      }
6027
6028      // For any resources, width and height must be provided, to avoid layout shifts.
6029      if ( ! isset( $attr['width'], $attr['height'] ) ) {
6030           /** This filter is documented in wp-includes/media.php */
6031           return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
6032      }
6033
6034      /*
6035       * The key function logic starts here.
6036       */
6037      $maybe_in_viewport    = null;
6038      $increase_count       = false;
6039      $maybe_increase_count = false;
6040
 
Line Code
6167       * Filters the loading optimization attributes.
6168       *
6169       * @since 6.4.0
6170       *
6171       * @param array  $loading_attrs The loading optimization attributes.
6172       * @param string $tag_name      The tag name.
6173       * @param array  $attr          Array of the attributes for the tag.
6174       * @param string $context       Context for the element for which the loading optimization attribute is requested.
6175       */
6176      return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
6177 }
6178
6179 /**
6180  * Gets the threshold for how many of the first content media elements to not lazy-load.
6181  *
6182  * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3.
6183  * The filter is only run once per page load, unless the `$force` parameter is used.
6184  *
6185  * @since 5.9.0