wp_mime_type_icon()
云策文档标注
概述
wp_mime_type_icon() 函数用于根据 MIME 类型或附件 ID 检索对应的图标。它支持指定首选文件格式,并利用缓存机制优化性能。
关键要点
- 参数 $mime 可以是字符串形式的 MIME 类型或整数形式的附件 ID,$preferred_ext 指定首选图标文件格式,默认为 '.png'。
- 函数首先检查缓存,若无缓存则根据 $mime 类型或附件 ID 获取相关 MIME 信息,并扫描图标目录匹配最佳图标。
- 支持多个过滤器钩子,如 icon_dir、icon_dirs、icon_dir_uri 和 wp_mime_type_icon,允许开发者自定义图标路径和输出。
- 返回图标路径字符串,若无匹配则返回 false。
代码示例
// 获取 MIME 类型 'image/jpeg' 的图标,首选 .png 格式
$icon = wp_mime_type_icon('image/jpeg', '.png');
if ($icon !== false) {
echo '<img src="' . esc_url($icon) . '" alt="Icon" />';
}
// 通过附件 ID 获取图标
$attachment_id = 123;
$icon = wp_mime_type_icon($attachment_id);
if ($icon !== false) {
echo '<img src="' . esc_url($icon) . '" alt="Icon" />';
}注意事项
- 函数内部使用 wp_cache_get() 和 wp_cache_add() 进行缓存,提高重复调用效率。
- 如果 $preferred_ext 参数不以 '.' 开头,函数会自动添加并转换为小写。
- 当 $mime 为附件 ID 时,函数会通过 get_post() 获取附件信息,并提取文件扩展名和 MIME 类型进行匹配。
- 图标目录默认位于 ABSPATH . WPINC . '/images/media',可通过过滤器修改。
原文内容
Retrieves the icon for a MIME type or attachment.
Parameters
$mimestring|intrequired-
MIME type or attachment ID.
$preferred_extstringrequired-
File format to prefer in return. Default
'.png'.
Source
function wp_mime_type_icon( $mime = 0, $preferred_ext = '.png' ) {
if ( ! is_numeric( $mime ) ) {
$icon = wp_cache_get( "mime_type_icon_$mime" );
}
// Check if preferred file format variable is present and is a validly formatted file extension.
if ( ! empty( $preferred_ext ) && is_string( $preferred_ext ) && ! str_starts_with( $preferred_ext, '.' ) ) {
$preferred_ext = '.' . strtolower( $preferred_ext );
}
$post_id = 0;
if ( empty( $icon ) ) {
$post_mimes = array();
if ( is_numeric( $mime ) ) {
$mime = (int) $mime;
$post = get_post( $mime );
if ( $post ) {
$post_id = (int) $post->ID;
$file = get_attached_file( $post_id );
$ext = preg_replace( '/^.+?.([^.]+)$/', '$1', $file );
if ( ! empty( $ext ) ) {
$post_mimes[] = $ext;
$ext_type = wp_ext2type( $ext );
if ( $ext_type ) {
$post_mimes[] = $ext_type;
}
}
$mime = $post->post_mime_type;
} else {
$mime = 0;
}
} else {
$post_mimes[] = $mime;
}
$icon_files = wp_cache_get( 'icon_files' );
if ( ! is_array( $icon_files ) ) {
/**
* Filters the icon directory path.
*
* @since 2.0.0
*
* @param string $path Icon directory absolute path.
*/
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
/**
* Filters the icon directory URI.
*
* @since 2.0.0
*
* @param string $uri Icon directory URI.
*/
$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
/**
* Filters the array of icon directory URIs.
*
* @since 2.5.0
*
* @param string[] $uris Array of icon directory URIs keyed by directory absolute path.
*/
$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
$icon_files = array();
$all_icons = array();
while ( $dirs ) {
$keys = array_keys( $dirs );
$dir = array_shift( $keys );
$uri = array_shift( $dirs );
$dh = opendir( $dir );
if ( $dh ) {
while ( false !== $file = readdir( $dh ) ) {
$file = wp_basename( $file );
if ( str_starts_with( $file, '.' ) ) {
continue;
}
$ext = strtolower( substr( $file, -4 ) );
if ( ! in_array( $ext, array( '.svg', '.png', '.gif', '.jpg' ), true ) ) {
if ( is_dir( "$dir/$file" ) ) {
$dirs[ "$dir/$file" ] = "$uri/$file";
}
continue;
}
$all_icons[ "$dir/$file" ] = "$uri/$file";
if ( $ext === $preferred_ext ) {
$icon_files[ "$dir/$file" ] = "$uri/$file";
}
}
closedir( $dh );
}
}
// If directory only contained icons of a non-preferred format, return those.
if ( empty( $icon_files ) ) {
$icon_files = $all_icons;
}
wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
}
$types = array();
// Icon wp_basename - extension = MIME wildcard.
foreach ( $icon_files as $file => $uri ) {
$types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ];
}
if ( ! empty( $mime ) ) {
$post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) );
$post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 );
$post_mimes[] = str_replace( '/', '_', $mime );
}
$matches = wp_match_mime_types( array_keys( $types ), $post_mimes );
$matches['default'] = array( 'default' );
foreach ( $matches as $match => $wilds ) {
foreach ( $wilds as $wild ) {
if ( ! isset( $types[ $wild ] ) ) {
continue;
}
$icon = $types[ $wild ];
if ( ! is_numeric( $mime ) ) {
wp_cache_add( "mime_type_icon_$mime", $icon );
}
break 2;
}
}
}
/**
* Filters the mime type icon.
*
* @since 2.1.0
*
* @param string $icon Path to the mime type icon.
* @param string $mime Mime type.
* @param int $post_id Attachment ID. Will equal 0 if the function passed
* the mime type.
*/
return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
}
Hooks
- apply_filters( ‘icon_dir’, string $path )
-
Filters the icon directory path.
- apply_filters( ‘icon_dirs’, string[] $uris )
-
Filters the array of icon directory URIs.
- apply_filters( ‘icon_dir_uri’, string $uri )
-
Filters the icon directory URI.
- apply_filters( ‘wp_mime_type_icon’, string $icon, string $mime, int $post_id )
-
Filters the mime type icon.
Skip to note 2 content
Codex
Display the icon image of the video format
[html]
<img src="” />
[/html]