feed_content_type()
云策文档标注
概述
feed_content_type() 函数用于根据指定的 feed 类型返回对应的内容类型(Content-Type)。它支持多种 feed 格式,并允许通过过滤器进行自定义。
关键要点
- 函数接受一个字符串参数 $type,指定 feed 类型,可选值包括 'rss'、'rss2'、'atom' 和 'rdf'。
- 如果 $type 为空,函数会使用 get_default_feed() 获取默认 feed 类型。
- 内部映射数组定义了 feed 类型到内容类型的对应关系,例如 'rss' 对应 'application/rss+xml'。
- 返回的内容类型可通过 'feed_content_type' 过滤器进行修改,允许开发者自定义输出。
- 函数在 WordPress 2.8.0 版本中引入,常用于处理 feed 相关的 HTTP 头设置。
代码示例
function feed_content_type( $type = '' ) {
if ( empty( $type ) ) {
$type = get_default_feed();
}
$types = array(
'rss' => 'application/rss+xml',
'rss2' => 'application/rss+xml',
'rss-http' => 'text/xml',
'atom' => 'application/atom+xml',
'rdf' => 'application/rdf+xml',
);
$content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream';
return apply_filters( 'feed_content_type', $content_type, $type );
}注意事项
- 如果指定的 $type 不在映射数组中,函数会返回默认的 'application/octet-stream'。
- 使用 apply_filters() 允许其他插件或主题通过 'feed_content_type' 钩子修改内容类型,增强了灵活性。
原文内容
Returns the content type for specified feed type.
Parameters
$typestringrequired-
Type of feed. Possible values include
'rss', rss2′,'atom', and'rdf'.
Source
function feed_content_type( $type = '' ) {
if ( empty( $type ) ) {
$type = get_default_feed();
}
$types = array(
'rss' => 'application/rss+xml',
'rss2' => 'application/rss+xml',
'rss-http' => 'text/xml',
'atom' => 'application/atom+xml',
'rdf' => 'application/rdf+xml',
);
$content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream';
/**
* Filters the content type for a specific feed type.
*
* @since 2.8.0
*
* @param string $content_type Content type indicating the type of data that a feed contains.
* @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'.
*/
return apply_filters( 'feed_content_type', $content_type, $type );
}
Hooks
- apply_filters( ‘feed_content_type’, string $content_type, string $type )
-
Filters the content type for a specific feed type.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |