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`屬性。

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