函数文档

wp_iframe_tag_add_loading_attr()

💡 云策文档标注

概述

wp_iframe_tag_add_loading_attr() 函数用于向 iframe HTML 标签添加 loading 属性,以优化加载性能。它通过检查 iframe 标签的 src、width 和 height 属性,并应用过滤器来动态设置 loading 值。

关键要点

  • 函数接受两个参数:$iframe(HTML iframe 标签字符串)和 $context(上下文信息),返回添加了 loading 属性的 iframe 标签字符串。
  • 仅当 iframe 标签包含 src、width 和 height 属性时,才会添加 loading 属性,否则返回原标签。
  • 使用 wp_get_loading_optimization_attributes() 获取优化属性,并通过 apply_filters('wp_iframe_tag_add_loading_attr') 过滤器允许自定义 loading 值。
  • loading 属性值默认为 'lazy',支持 'lazy' 或 'eager',无效值会被重置为 'lazy'。
  • 该函数自 WordPress 5.7.0 引入,主要用于 wp_filter_content_tags() 中处理内容标签。

代码示例

function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
    $optimization_attrs = wp_get_loading_optimization_attributes(
        'iframe',
        array(
            'width'   => str_contains( $iframe, ' width="' ) ? 100 : null,
            'height'  => str_contains( $iframe, ' height="' ) ? 100 : null,
            'loading' => null,
        ),
        $context
    );

    if ( ! str_contains( $iframe, ' src="' ) || ! str_contains( $iframe, ' width="' ) || ! str_contains( $iframe, ' height="' ) ) {
        return $iframe;
    }

    $value = isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false;
    $value = apply_filters( 'wp_iframe_tag_add_loading_attr', $value, $iframe, $context );

    if ( $value ) {
        if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
            $value = 'lazy';
        }
        return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe );
    }

    return $iframe;
}

注意事项

  • 如果 iframe 标签已包含 loading 属性,此函数不会被调用。
  • 过滤器 wp_iframe_tag_add_loading_attr 允许返回 false 或空字符串来跳过添加属性,或返回 true 使用默认值。
  • 当前实现中,width 和 height 的临时值用于兼容性,未来可能使用 WP_HTML_Tag_Processor 提取实际值。

📄 原文内容

Adds loading attribute to an iframe HTML tag.

Parameters

$iframestringrequired
The HTML iframe tag where the attribute should be added.
$contextstringrequired
Additional context to pass to the filters.

Return

string Converted iframe tag with loading attribute added.

Source

function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
	/*
	 * Get loading attribute value to use. This must occur before the conditional check below so that even iframes that
	 * are ineligible for being lazy-loaded are considered.
	 */
	$optimization_attrs = wp_get_loading_optimization_attributes(
		'iframe',
		array(
			/*
			 * The concrete values for width and height are not important here for now
			 * since fetchpriority is not yet supported for iframes.
			 * TODO: Use WP_HTML_Tag_Processor to extract actual values once support is
			 * added.
			 */
			'width'   => str_contains( $iframe, ' width="' ) ? 100 : null,
			'height'  => str_contains( $iframe, ' height="' ) ? 100 : null,
			// This function is never called when a 'loading' attribute is already present.
			'loading' => null,
		),
		$context
	);

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

	$value = isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false;

	/**
	 * Filters the `loading` attribute value to add to an iframe. Default `lazy`.
	 *
	 * Returning `false` or an empty string will not add the attribute.
	 * Returning `true` will add the default value.
	 *
	 * @since 5.7.0
	 *
	 * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
	 *                             the attribute being omitted for the iframe.
	 * @param string      $iframe  The HTML `iframe` tag to be filtered.
	 * @param string      $context Additional context about how the function was called or where the iframe tag is.
	 */
	$value = apply_filters( 'wp_iframe_tag_add_loading_attr', $value, $iframe, $context );

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

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

	return $iframe;
}

Hooks

apply_filters( ‘wp_iframe_tag_add_loading_attr’, string|bool $value, string $iframe, string $context )

Filters the loading attribute value to add to an iframe. Default lazy.

Changelog

Version Description
5.7.0 Introduced.