get_header_video_url()
云策文档标注
概述
get_header_video_url() 函数用于检索自定义页眉的视频 URL,优先使用本地视频,若无则回退到外部视频。
关键要点
- 函数返回字符串类型的视频 URL 或 false(无视频时)。
- 通过 get_theme_mod('header_video') 获取本地视频附件 ID,使用 wp_get_attachment_url() 转换为 URL。
- 若本地视频不存在,则通过 get_theme_mod('external_header_video') 获取外部视频 URL。
- 应用过滤器 get_header_video_url 允许自定义 URL。
- 最终返回经过 sanitize_url() 和 set_url_scheme() 处理的 URL。
代码示例
function get_header_video_url() {
$id = absint( get_theme_mod( 'header_video' ) );
if ( $id ) {
$url = wp_get_attachment_url( $id );
} else {
$url = get_theme_mod( 'external_header_video' );
}
$url = apply_filters( 'get_header_video_url', $url );
if ( ! $id && ! $url ) {
return false;
}
return sanitize_url( set_url_scheme( $url ) );
}注意事项
- 函数自 WordPress 4.7.0 版本引入。
- 相关函数包括 has_header_video()、the_header_video_url() 和 get_header_video_settings()。
- 依赖的辅助函数如 get_theme_mod()、set_url_scheme()、wp_get_attachment_url()、sanitize_url()、absint() 和 apply_filters()。
原文内容
Retrieves header video URL for custom header.
Description
Uses a local video if present, or falls back to an external video.
Source
function get_header_video_url() {
$id = absint( get_theme_mod( 'header_video' ) );
if ( $id ) {
// Get the file URL from the attachment ID.
$url = wp_get_attachment_url( $id );
} else {
$url = get_theme_mod( 'external_header_video' );
}
/**
* Filters the header video URL.
*
* @since 4.7.3
*
* @param string $url Header video URL, if available.
*/
$url = apply_filters( 'get_header_video_url', $url );
if ( ! $id && ! $url ) {
return false;
}
return sanitize_url( set_url_scheme( $url ) );
}
Hooks
- apply_filters( ‘get_header_video_url’, string $url )
-
Filters the header video URL.
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |