wp_iframe_tag_add_loading_attr

函数
wp_iframe_tag_add_loading_attr ( $iframe, $context )
参数
  • (string) $iframe The HTML `iframe` tag where the attribute should be added.
    Required:
  • (string) $context Additional context to pass to the filters.
    Required:
返回值
  • (string) Converted `iframe` tag with `loading` attribute added.
定义位置
相关方法
wp_img_tag_add_loading_attrwp_img_tag_add_decoding_attrwp_img_tag_add_width_and_height_attrwp_img_tag_add_srcset_and_sizes_attrwp_ajax_add_link_category
引入
5.7.0
弃用
-

wp_iframe_tag_add_loading_attr: 这个函数用来给一个HTML iframe元素添加一个加载属性。它接受一个参数,即iframe元素的HTML代码,并返回添加了加载属性的修改后的代码。

为HTML标签`iframe`添加`loading`属性。

function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
	// Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are
	// visually hidden initially.
	if ( false !== strpos( $iframe, ' data-secret="' ) ) {
		return $iframe;
	}

	// 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.
	$value = wp_get_loading_attr_default( $context );

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

	/**
	 * 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;
}

常见问题

FAQs
查看更多 >