atom_enclosure()
云策文档标注
概述
atom_enclosure() 函数用于显示当前文章的 Atom 附件。它检查文章是否需要密码保护,并解析 'enclosure' 元数据字段以输出链接 HTML 标签。
关键要点
- 函数基于全局 $post 检查密码保护,若未提供正确密码则直接返回。
- 使用 get_post_custom() 获取 'enclosure' 元数据,解析为 URL、类型和长度。
- 输出经过 esc_url() 和 esc_attr() 转义的 HTML 链接标签,并应用 atom_enclosure 过滤器。
代码示例
function atom_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 );
$url = '';
$type = '';
$length = 0;
$mimes = get_allowed_mime_types();
// Parse URL.
if ( isset( $enclosure[0] ) && is_string( $enclosure[0] ) ) {
$url = trim( $enclosure[0] );
}
// Parse length and type.
for ( $i = 1; $i < 3; $i++ ) {
if ( isset( $enclosure[$i] ) ) {
switch ( $i ) {
case 1:
$length = (int) $enclosure[$i];
break;
case 2:
if ( in_array( $enclosure[$i], $mimes, true ) ) {
$type = $enclosure[$i];
}
break;
}
}
}
$html_link_tag = sprintf(
"<link href='%s' length='%s' type='%s' />n",
esc_url( $url ),
esc_attr( $length ),
esc_attr( $type )
);
/**
* Filters the atom 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( 'atom_enclosure', $html_link_tag );
}
}
}
}注意事项
- 函数在 WordPress 2.2.0 版本中引入,主要用于 Atom 订阅格式。
- 依赖多个核心函数如 get_allowed_mime_types() 和 post_password_required(),确保环境兼容。
- 输出前应用 atom_enclosure 过滤器,允许开发者自定义 HTML 链接标签。
原文内容
Displays the atom 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 link HTML tag(s) with a URI and other attributes.
Source
function atom_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 );
$url = '';
$type = '';
$length = 0;
$mimes = get_allowed_mime_types();
// Parse URL.
if ( isset( $enclosure[0] ) && is_string( $enclosure[0] ) ) {
$url = trim( $enclosure[0] );
}
// Parse length and type.
for ( $i = 1; $i <= 2; $i++ ) {
if ( isset( $enclosure[ $i ] ) ) {
if ( is_numeric( $enclosure[ $i ] ) ) {
$length = trim( $enclosure[ $i ] );
} elseif ( in_array( $enclosure[ $i ], $mimes, true ) ) {
$type = trim( $enclosure[ $i ] );
}
}
}
$html_link_tag = sprintf(
"<link href="%s" rel="enclosure" length="%d" type="%s" />n",
esc_url( $url ),
esc_attr( $length ),
esc_attr( $type )
);
/**
* Filters the atom 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( 'atom_enclosure', $html_link_tag );
}
}
}
}
Hooks
- apply_filters( ‘atom_enclosure’, string $html_link_tag )
-
Filters the atom enclosure HTML link tag for the current post.
Changelog
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |