wp_ext2type()
云策文档标注
概述
wp_ext2type() 函数用于根据文件扩展名检索对应的文件类型,如音频、视频、文档等。它通过内部调用 wp_get_ext_types() 获取扩展名与类型的映射关系进行匹配。
关键要点
- 参数 $ext 为必需,表示要搜索的文件扩展名
- 返回值为字符串类型,如 audio、video、document、spreadsheet 等,若未找到则可能返回 void
- 函数内部将扩展名转换为小写后,遍历 wp_get_ext_types() 返回的数组进行匹配
- 该函数自 WordPress 2.5.0 版本引入,常用于媒体上传、AJAX 操作和 MIME 类型图标处理等场景
代码示例
function wp_ext2type( $ext ) {
$ext = strtolower( $ext );
$ext2type = wp_get_ext_types();
foreach ( $ext2type as $type => $exts ) {
if ( in_array( $ext, $exts, true ) ) {
return $type;
}
}
}注意事项
- 扩展名匹配区分大小写,函数内部已处理为小写,但输入时建议保持一致
- 若扩展名未在 wp_get_ext_types() 定义的列表中,函数可能不返回值(void),调用时需注意处理
- 相关函数包括 wp_get_ext_types()、wp_media_upload_handler()、wp_ajax_send_link_to_editor() 和 wp_mime_type_icon(),用于扩展功能集成
原文内容
Retrieves the file type based on the extension name.
Parameters
$extstringrequired-
The extension to search.
Source
function wp_ext2type( $ext ) {
$ext = strtolower( $ext );
$ext2type = wp_get_ext_types();
foreach ( $ext2type as $type => $exts ) {
if ( in_array( $ext, $exts, true ) ) {
return $type;
}
}
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |