wp_iframe_tag_add_loading_attr

The timeline below displays how wordpress function wp_iframe_tag_add_loading_attr has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Adds `loading` attribute to an `iframe` HTML tag.
 *
 * @since 5.7.0
 *
 * @param string $iframe  The HTML `iframe` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `iframe` tag with `loading` attribute added.
 */
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 (str_contains($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.
     */
    $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;
}

WordPress Version: 5.9

/**
 * Adds `loading` attribute to an `iframe` HTML tag.
 *
 * @since 5.7.0
 *
 * @param string $iframe  The HTML `iframe` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `iframe` tag with `loading` attribute added.
 */
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;
}

WordPress Version: 7.1

/**
 * Adds `loading` attribute to an `iframe` HTML tag.
 *
 * @since 5.7.0
 *
 * @param string $iframe  The HTML `iframe` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `iframe` tag with `loading` attribute added.
 */
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;
    }
    // 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. Default 'lazy'.
     * @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', 'lazy', $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;
}

WordPress Version: 5.7

/**
 * Adds `loading` attribute to an `iframe` HTML tag.
 *
 * @since 5.7.0
 *
 * @param string $iframe  The HTML `iframe` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `iframe` tag with `loading` attribute added.
 */
function wp_iframe_tag_add_loading_attr($iframe, $context)
{
    /**
     * 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. Default 'lazy'.
     * @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', 'lazy', $iframe, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        // 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;
        }
        return str_replace('<iframe', '<iframe loading="' . esc_attr($value) . '"', $iframe);
    }
    return $iframe;
}