
什么是Caniuse及如何使用它提升网站
wp_img_tag_add_decoding_attr ( $image, $context )
wp_img_tag_add_decoding_attr是一个为HTML img标签添加解码属性的函数。解码属性告诉浏览器如何加载图片。”async”表示异步加载,”sync”表示同步加载,或者”auto”表示让浏览器决定。这可以通过减少页面的加载时间来帮助提高性能。
为`img’HTML标签添加`decoding’属性。
`decoding`属性允许开发者指示浏览器是否可以在主线程之外(`async`)、在主线程上(`sync`)或由浏览器决定(`auto`)对图片进行解码。
默认情况下,WordPress会给图片添加`decoding=””async””`,但开发者可以使用{@see ‘wp_img_tag_add_decoding_attr’}过滤器来修改,删除该属性或将其设置为其他可接受的值。
function wp_img_tag_add_decoding_attr( $image, $context ) { /** * Filters the `decoding` attribute value to add to an image. Default `async`. * * Returning a falsey value will omit the attribute. * * @since 6.1.0 * * @param string|false|null $value The `decoding` attribute value. Returning a falsey value * will result in the attribute being omitted for the image. * Otherwise, it may be: 'async' (default), 'sync', or 'auto'. * @param string $image The HTML `img` tag to be filtered. * @param string $context Additional context about how the function was called * or where the img tag is. */ $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context ); if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) { $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image ); } return $image; }