rss_enclosure()
云策文档标注
概述
rss_enclosure() 函数用于显示当前文章的 RSS 附件。它检查文章是否需要密码保护,并基于文章的 'enclosure' 元数据字段生成 HTML 链接标签。
关键要点
- 函数使用全局 $post 检查密码保护,若未提供正确密码则直接返回。
- 通过 get_post_custom() 获取 'enclosure' 元数据,解析并输出包含 URI 和其他属性的 HTML 标签。
- 提供 apply_filters('rss_enclosure', string $html_link_tag) Hook,允许过滤输出的 HTML 链接标签。
代码示例
function rss_enclosure() {
if ( post_password_required() ) {
return;
}
foreach ( (array) get_post_custom() as $key => $val ) {
if ( 'enclosure' === $key ) {
foreach ( (array) $val as $enc ) {
$enclosure = explode( "n", $enc );
if ( count( $enclosure ) ' . "n" );
}
}
}
}
}注意事项
- 函数依赖于 post_password_required() 进行密码检查,确保安全访问。
- 使用 esc_url()、esc_attr() 和 absint() 等函数对输出进行转义和清理,防止安全漏洞。
- 相关函数包括 get_post_custom()、post_password_required() 和 apply_filters(),用于元数据获取和过滤。
原文内容
Displays the rss enclosure for the current post.
Description
Uses the global $post to check whether the post requires a password and if the user has the password for the post. If not then it will return before displaying.
Also uses the function get_post_custom() to get the post’s ‘enclosure’ metadata field and parses the value to display the enclosure(s). The enclosure(s) consist of enclosure HTML tag(s) with a URI and other attributes.
Source
function rss_enclosure() {
if ( post_password_required() ) {
return;
}
foreach ( (array) get_post_custom() as $key => $val ) {
if ( 'enclosure' === $key ) {
foreach ( (array) $val as $enc ) {
$enclosure = explode( "n", $enc );
if ( count( $enclosure ) < 3 ) {
continue;
}
// Only get the first element, e.g. 'audio/mpeg' from 'audio/mpeg mpga mp2 mp3'.
$t = preg_split( '/[ t]/', trim( $enclosure[2] ) );
$type = $t[0];
/**
* Filters the RSS enclosure HTML link tag for the current post.
*
* @since 2.2.0
*
* @param string $html_link_tag The HTML link tag with a URI and other attributes.
*/
echo apply_filters( 'rss_enclosure', '<enclosure url="' . esc_url( trim( $enclosure[0] ) ) . '" length="' . absint( trim( $enclosure[1] ) ) . '" type="' . esc_attr( $type ) . '" />' . "n" );
}
}
}
}
Hooks
- apply_filters( ‘rss_enclosure’, string $html_link_tag )
-
Filters the RSS enclosure HTML link tag for the current post.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |