函数文档

wp_filter_oembed_iframe_title_attribute()

💡 云策文档标注

概述

wp_filter_oembed_iframe_title_attribute() 是一个 WordPress 过滤器函数,用于确保 oEmbed HTML 中的 iframe 元素具有 title 属性。它处理来自 oEmbed 提供程序的数据,并应用相关过滤器以增强可访问性。

关键要点

  • 函数检查 oEmbed 结果是否为 rich 或 video 类型,否则直接返回原结果。
  • 通过正则表达式解析 iframe 属性,处理大小写不一致问题,并优先使用现有 title 属性值。
  • 提供 oembed_iframe_title_attribute 过滤器,允许开发者自定义 title 属性的生成逻辑。
  • 如果 title 为空,则返回原始 oEmbed HTML;否则更新 iframe 属性并返回过滤后的结果。

代码示例

function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) {
    if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) {
        return $result;
    }

    $title = ! empty( $data->title ) ? $data->title : '';

    $pattern = '`<iframe([^>]*)>`i';
    if ( preg_match( $pattern, $result, $matches ) ) {
        $attrs = wp_kses_hair( $matches[1], wp_allowed_protocols() );

        foreach ( $attrs as $attr => $item ) {
            $lower_attr = strtolower( $attr );
            if ( $lower_attr === $attr ) {
                continue;
            }
            if ( ! isset( $attrs[ $lower_attr ] ) ) {
                $attrs[ $lower_attr ] = $item;
                unset( $attrs[ $attr ] );
            }
        }
    }

    if ( ! empty( $attrs['title']['value'] ) ) {
        $title = $attrs['title']['value'];
    }

    $title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url );

    if ( '' === $title ) {
        return $result;
    }

    if ( isset( $attrs['title'] ) ) {
        unset( $attrs['title'] );
        $attr_string = implode( ' ', wp_list_pluck( $attrs, 'whole' ) );
        $result      = str_replace( $matches[0], '<iframe ' . $attr_string . ' title="' . esc_attr( $title ) . '">', $result );
    }
    return $result;
}

注意事项

  • 函数仅处理 rich 或 video 类型的 oEmbed 结果,其他类型如 link 或 photo 会被忽略。
  • 使用 wp_kses_hair() 和 wp_allowed_protocols() 确保属性解析的安全性。
  • 通过 oembed_iframe_title_attribute 过滤器,开发者可以修改 title 值,但需注意转义以避免 XSS 风险。
  • 自 WordPress 5.2.0 版本引入,相关函数如 wp_list_pluck() 和 esc_attr() 用于辅助处理。

📄 原文内容

Filters the given oEmbed HTML to make sure iframes have a title attribute.

Parameters

$resultstring|falserequired
The oEmbed HTML result.
$dataobjectrequired
A data object result from an oEmbed provider.
$urlstringrequired
The URL of the content to be embedded.

Return

string|false The filtered oEmbed result.

Source

function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) {
	if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) {
		return $result;
	}

	$title = ! empty( $data->title ) ? $data->title : '';

	$pattern = '`<iframe([^>]*)>`i';
	if ( preg_match( $pattern, $result, $matches ) ) {
		$attrs = wp_kses_hair( $matches[1], wp_allowed_protocols() );

		foreach ( $attrs as $attr => $item ) {
			$lower_attr = strtolower( $attr );
			if ( $lower_attr === $attr ) {
				continue;
			}
			if ( ! isset( $attrs[ $lower_attr ] ) ) {
				$attrs[ $lower_attr ] = $item;
				unset( $attrs[ $attr ] );
			}
		}
	}

	if ( ! empty( $attrs['title']['value'] ) ) {
		$title = $attrs['title']['value'];
	}

	/**
	 * Filters the title attribute of the given oEmbed HTML iframe.
	 *
	 * @since 5.2.0
	 *
	 * @param string $title  The title attribute.
	 * @param string $result The oEmbed HTML result.
	 * @param object $data   A data object result from an oEmbed provider.
	 * @param string $url    The URL of the content to be embedded.
	 */
	$title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url );

	if ( '' === $title ) {
		return $result;
	}

	if ( isset( $attrs['title'] ) ) {
		unset( $attrs['title'] );
		$attr_string = implode( ' ', wp_list_pluck( $attrs, 'whole' ) );
		$result      = str_replace( $matches[0], '<iframe ' . trim( $attr_string ) . '>', $result );
	}
	return str_ireplace( '<iframe ', sprintf( '<iframe title="%s" ', esc_attr( $title ) ), $result );
}

Hooks

apply_filters( ‘oembed_iframe_title_attribute’, string $title, string $result, object $data, string $url )

Filters the title attribute of the given oEmbed HTML iframe.

Changelog

Version Description
5.2.0 Introduced.