函数文档

wp_embed_handler_video()

💡 云策文档标注

概述

wp_embed_handler_video() 是 WordPress 中用于处理视频嵌入的回调函数,它根据提供的参数生成视频嵌入的 HTML 代码,并允许通过过滤器进行自定义修改。

关键要点

  • 函数作为 wp_embed_register_handler() 的回调,处理视频 URL 的嵌入匹配。
  • 接受四个参数:$matches(正则匹配结果)、$attr(嵌入属性)、$url(原始 URL)、$rawattr(原始未修改属性)。
  • 返回视频嵌入的 HTML 字符串,支持通过 wp_embed_handler_video 过滤器进行输出过滤。
  • 函数内部处理宽度和高度属性,并生成包含这些属性的视频标签。

代码示例

function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
    $dimensions = '';
    if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
        $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
        $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
    }
    $video = sprintf( '"%s"', $dimensions, esc_url( $url ) );
    return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
}

注意事项

  • 函数在 WordPress 3.6.0 版本中引入,是核心嵌入系统的一部分。
  • 使用 esc_url() 对 URL 进行清理,确保安全性。
  • 相关函数包括 esc_url() 和 apply_filters(),用于 URL 处理和过滤器应用。

📄 原文内容

Video embed handler callback.

Parameters

$matchesarrayrequired
The RegEx matches from the provided regex when calling wp_embed_register_handler() .
$attrarrayrequired
Embed attributes.
$urlstringrequired
The original URL that was matched by the regex.
$rawattrarrayrequired
The original unmodified attributes.

Return

string The embed HTML.

Source

function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
	$dimensions = '';
	if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
		$dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
		$dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
	}
	$video = sprintf( '<a class="wp-embedded-video" href="http://"%s"">"%s"</a>', $dimensions, esc_url( $url ) );

	/**
	 * Filters the video embed output.
	 *
	 * @since 3.6.0
	 *
	 * @param string $video   Video embed output.
	 * @param array  $attr    An array of embed attributes.
	 * @param string $url     The original URL that was matched by the regex.
	 * @param array  $rawattr The original unmodified attributes.
	 */
	return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
}

Hooks

apply_filters( ‘wp_embed_handler_video’, string $video, array $attr, string $url, array $rawattr )

Filters the video embed output.

Changelog

Version Description
3.6.0 Introduced.