wp_is_xml_request()
云策文档标注
概述
wp_is_xml_request() 函数用于检测当前请求是否为 XML 请求或期望 XML 响应。它通过检查 HTTP 头中的 Accept 或 Content-Type 字段是否包含特定的 XML MIME 类型来实现。
关键要点
- 函数返回布尔值:如果 Accept 或 Content-Type 头包含 text/xml 或相关 MIME 类型则返回 true,否则返回 false。
- 支持的 MIME 类型包括 text/xml、application/rss+xml、application/atom+xml、application/rdf+xml、text/xml+oembed 和 application/xml+oembed。
- 该函数自 WordPress 5.2.0 版本引入。
代码示例
function wp_is_xml_request() {
$accepted = array(
'text/xml',
'application/rss+xml',
'application/atom+xml',
'application/rdf+xml',
'text/xml+oembed',
'application/xml+oembed',
);
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
foreach ( $accepted as $type ) {
if ( str_contains( $_SERVER['HTTP_ACCEPT'], $type ) ) {
return true;
}
}
}
if ( isset( $_SERVER['CONTENT_TYPE'] ) && in_array( $_SERVER['CONTENT_TYPE'], $accepted, true ) ) {
return true;
}
return false;
}
原文内容
Checks whether current request is an XML request, or is expecting an XML response.
Source
function wp_is_xml_request() {
$accepted = array(
'text/xml',
'application/rss+xml',
'application/atom+xml',
'application/rdf+xml',
'text/xml+oembed',
'application/xml+oembed',
);
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
foreach ( $accepted as $type ) {
if ( str_contains( $_SERVER['HTTP_ACCEPT'], $type ) ) {
return true;
}
}
}
if ( isset( $_SERVER['CONTENT_TYPE'] ) && in_array( $_SERVER['CONTENT_TYPE'], $accepted, true ) ) {
return true;
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 5.2.0 | Introduced. |