Make WordPress Core


Ignore:
Timestamp:
09/21/2023 04:35:30 PM (3 years ago)
Author:
flixos90
Message:

Media: Introduce filters to customize the results from wp_get_loading_optimization_attributes().

This changeset introduces two filters that allow customizing the loading optimization attributes array returned from wp_get_loading_optimization_attributes() for individual HTML tags:

  • The wp_get_loading_optimization_attributes filter can be used to modify the results from the WordPress core logic.
  • The pre_wp_get_loading_optimization_attributes filter can be used to use entirely custom logic and effectively short-circuit the core function.

Props pereirinha, mukesh27, spacedmonkey, joemcgill.
Fixes #58893.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r56612 r56651  
    56265626        global $wp_query;
    56275627
     5628        /**
     5629         * Filters whether to short-circuit loading optimization attributes.
     5630         *
     5631         * Returning an array from the filter will effectively short-circuit the loading of optimization attributes,
     5632         * returning that value instead.
     5633         *
     5634         * @since 6.4.0
     5635         *
     5636         * @param array|false $loading_attrs False by default, or array of loading optimization attributes to short-circuit.
     5637         * @param string      $tag_name      The tag name.
     5638         * @param array       $attr          Array of the attributes for the tag.
     5639         * @param string      $context       Context for the element for which the loading optimization attribute is requested.
     5640         */
     5641        $loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', false, $tag_name, $attr, $context );
     5642
     5643        if ( is_array( $loading_attrs ) ) {
     5644                return $loading_attrs;
     5645        }
     5646
    56285647        $loading_attrs = array();
    56295648
    … …  
    56335652         */
    56345653        if ( 'template' === $context ) {
    5635                 return $loading_attrs;
     5654                /** This filter is documented in wp-includes/media.php */
     5655                return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
    56365656        }
    56375657
    56385658        // For now this function only supports images and iframes.
    56395659        if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
    5640                 return $loading_attrs;
     5660                /** This filter is documented in wp-includes/media.php */
     5661                return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
    56415662        }
    56425663
    56435664        // For any resources, width and height must be provided, to avoid layout shifts.
    56445665        if ( ! isset( $attr['width'], $attr['height'] ) ) {
    5645                 return $loading_attrs;
     5666                /** This filter is documented in wp-includes/media.php */
     5667                return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
    56465668        }
    56475669
    … …  
    56555677        // TODO: Handle shortcode images together with the content (see https://core.trac.wordpress.org/ticket/58853).
    56565678        if ( 'the_content' !== $context && 'do_shortcode' !== $context && doing_filter( 'the_content' ) ) {
    5657                 return $loading_attrs;
     5679                /** This filter is documented in wp-includes/media.php */
     5680                return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
    56585681        }
    56595682
    … …  
    57905813        }
    57915814
    5792         return $loading_attrs;
     5815        /**
     5816         * Filters the loading optimization attributes.
     5817         *
     5818         * @since 6.4.0
     5819         *
     5820         * @param array  $loading_attrs The loading optimization attributes.
     5821         * @param string $tag_name      The tag name.
     5822         * @param array  $attr          Array of the attributes for the tag.
     5823         * @param string $context       Context for the element for which the loading optimization attribute is requested.
     5824         */
     5825        return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
    57935826}
    57945827
Note: See TracChangeset for help on using the changeset viewer.