wp_img_tag_add_decoding_attr

函式
wp_img_tag_add_decoding_attr ( $image, $context )
引數
  • (string) $image The HTML `img` tag where the attribute should be added.
    Required:
  • (string) $context Additional context to pass to the filters.
    Required:
返回值
  • (string) Converted `img` tag with `decoding` attribute added.
定義位置
相關方法
wp_img_tag_add_loading_attrwp_iframe_tag_add_loading_attrwp_img_tag_add_width_and_height_attrwp_img_tag_add_srcset_and_sizes_attrwp_script_add_data
引入
6.1.0
棄用
-

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’}過濾器來修改,刪除該屬性或將其設定為其他可接受的值。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >