wp_img_tag_add_loading_attr

函式
wp_img_tag_add_loading_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 `loading` attribute added.
定義位置
相關方法
wp_img_tag_add_decoding_attrwp_iframe_tag_add_loading_attrwp_img_tag_add_width_and_height_attrwp_img_tag_add_srcset_and_sizes_attrwp_get_loading_attr_default
引入
5.5.0
棄用
-

wp_img_tag_add_loading_attr是一個給HTML img標籤新增載入屬性的函式。載入屬性指定了圖片的載入方式,它可以取三個值。”eager”表示立即載入,”lazy”表示只在影象進入檢視時載入,”auto”表示讓瀏覽器決定。

為`img`HTML標籤新增`loading`屬性。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_img_tag_add_loading_attr( $image, $context ) {
// Get loading attribute value to use. This must occur before the conditional check below so that even images that
// are ineligible for being lazy-loaded are considered.
$value = wp_get_loading_attr_default( $context );
// Images should have source and dimension attributes for the `loading` attribute to be added.
if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
return $image;
}
/**
* Filters the `loading` attribute value to add to an image. Default `lazy`.
*
* Returning `false` or an empty string will not add the attribute.
* Returning `true` will add the default value.
*
* @since 5.5.0
*
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in
* the attribute being omitted for the image.
* @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_loading_attr', $value, $image, $context );
if ( $value ) {
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
$value = 'lazy';
}
return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
}
return $image;
}
function wp_img_tag_add_loading_attr( $image, $context ) { // Get loading attribute value to use. This must occur before the conditional check below so that even images that // are ineligible for being lazy-loaded are considered. $value = wp_get_loading_attr_default( $context ); // Images should have source and dimension attributes for the `loading` attribute to be added. if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { return $image; } /** * Filters the `loading` attribute value to add to an image. Default `lazy`. * * Returning `false` or an empty string will not add the attribute. * Returning `true` will add the default value. * * @since 5.5.0 * * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in * the attribute being omitted for the image. * @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_loading_attr', $value, $image, $context ); if ( $value ) { if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { $value = 'lazy'; } return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image ); } return $image; }
function wp_img_tag_add_loading_attr( $image, $context ) {
	// Get loading attribute value to use. This must occur before the conditional check below so that even images that
	// are ineligible for being lazy-loaded are considered.
	$value = wp_get_loading_attr_default( $context );

	// Images should have source and dimension attributes for the `loading` attribute to be added.
	if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) {
		return $image;
	}

	/**
	 * Filters the `loading` attribute value to add to an image. Default `lazy`.
	 *
	 * Returning `false` or an empty string will not add the attribute.
	 * Returning `true` will add the default value.
	 *
	 * @since 5.5.0
	 *
	 * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
	 *                             the attribute being omitted for the image.
	 * @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_loading_attr', $value, $image, $context );

	if ( $value ) {
		if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
			$value = 'lazy';
		}

		return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image );
	}

	return $image;
}

常見問題

FAQs
檢視更多 >