函数文档

wp_embed_handler_audio()

💡 云策文档标注

概述

wp_embed_handler_audio() 是 WordPress 中用于处理音频嵌入的回调函数,它根据正则匹配结果生成音频嵌入的 HTML 输出,并可通过过滤器进行自定义。

关键要点

  • 函数接受四个参数:$matches(正则匹配数组)、$attr(嵌入属性数组)、$url(原始 URL)、$rawattr(原始未修改属性数组)。
  • 返回字符串形式的音频嵌入 HTML,使用 esc_url() 清理 URL 以确保安全性。
  • 通过 apply_filters('wp_embed_handler_audio', ...) 提供过滤器,允许开发者修改音频嵌入输出。
  • 自 WordPress 3.6.0 版本引入,是 wp_embed_register_handler() 注册处理程序的一部分。

代码示例

function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) {
    $audio = sprintf( '"%s"', esc_url( $url ) );
    return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr );
}

注意事项

  • 函数内部使用 esc_url() 对 URL 进行转义,防止 XSS 攻击,确保输出安全。
  • 过滤器 wp_embed_handler_audio 允许在运行时动态调整音频嵌入内容,参数包括 $audio、$attr、$url 和 $rawattr。
  • 此函数通常不直接调用,而是通过 wp_embed_register_handler() 注册为音频 URL 的嵌入处理程序。

📄 原文内容

Audio 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_audio( $matches, $attr, $url, $rawattr ) {
	$audio = sprintf( '<a class="wp-embedded-audio" href="http://"%s"">"%s"</a>', esc_url( $url ) );

	/**
	 * Filters the audio embed output.
	 *
	 * @since 3.6.0
	 *
	 * @param string $audio   Audio 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_audio', $audio, $attr, $url, $rawattr );
}

Hooks

apply_filters( ‘wp_embed_handler_audio’, string $audio, array $attr, string $url, array $rawattr )

Filters the audio embed output.

Changelog

Version Description
3.6.0 Introduced.