wp_mediaelement_fallback()
云策文档标注
概述
wp_mediaelement_fallback() 函数用于为音频/视频元素提供无 JavaScript 时的 Flash 后备方案,确保媒体内容在旧浏览器或禁用 JS 的环境中仍可访问。它生成并返回包含媒体 URL 的 HTML 字符串。
关键要点
- 函数接受一个必需参数 $url,表示媒体文件的 URL。
- 返回一个字符串,包含用于无 JS 环境的 HTML 后备输出。
- 通过 apply_filters('wp_mediaelement_fallback', $output, $url) Hook 允许开发者过滤输出。
- 主要用于 wp_video_shortcode() 和 wp_audio_shortcode() 函数中,以增强媒体短码的兼容性。
- 自 WordPress 3.6.0 版本引入,是核心媒体功能的一部分。
代码示例
$fallback = wp_mediaelement_fallback( '/wp-content/uploads/2013/06/my-audio-file.mp3' );
echo $fallback;注意事项
此函数依赖于 esc_url() 来清理 URL,确保安全性;使用时需确保传入有效的媒体文件路径。
原文内容
Provides a No-JS Flash fallback as a last resort for audio / video.
Parameters
$urlstringrequired-
The media element URL.
Source
function wp_mediaelement_fallback( $url ) {
/**
* Filters the MediaElement fallback output for no-JS.
*
* @since 3.6.0
*
* @param string $output Fallback output for no-JS.
* @param string $url Media file URL.
*/
return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
}
Hooks
- apply_filters( ‘wp_mediaelement_fallback’, string $output, string $url )
-
Filters the MediaElement fallback output for no-JS.
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
Skip to note 2 content
Codex
Basic Example
$fallback = wp_mediaelement_fallback( '/wp-content/uploads/2013/06/my-audio-file.mp3' ); echo $fallback;Returns this HTML:
<a href="/wp-content/uploads/2013/06/my-audio-file.mp3">/wp-content/uploads/2013/06/my-audio-file.mp3</a>