wp_mime_type_icon

Retrieve the icon for a MIME type.

Description

(string|false) wp_mime_type_icon( (int) $mime = 0 ); 

Returns (string|false)

Icon, false otherwise.

Parameters (1)

0. $mime — Optional. (int)
MIME type or attachment ID.

Usage

  1. if ( !function_exists( 'wp_mime_type_icon' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // MIME type or attachment ID. 
  5. $mime = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_mime_type_icon($mime); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function wp_mime_type_icon( $mime = 0 ) { 
  2. if ( !is_numeric($mime) ) 
  3. $icon = wp_cache_get("mime_type_icon_$mime"); 
  4.  
  5. $post_id = 0; 
  6. if ( empty($icon) ) { 
  7. $post_mimes = array(); 
  8. if ( is_numeric($mime) ) { 
  9. $mime = (int) $mime; 
  10. if ( $post = get_post( $mime ) ) { 
  11. $post_id = (int) $post->ID; 
  12. $file = get_attached_file( $post_id ); 
  13. $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $file); 
  14. if ( !empty($ext) ) { 
  15. $post_mimes[] = $ext; 
  16. if ( $ext_type = wp_ext2type( $ext ) ) 
  17. $post_mimes[] = $ext_type; 
  18. $mime = $post->post_mime_type; 
  19. } else { 
  20. $mime = 0; 
  21. } else { 
  22. $post_mimes[] = $mime; 
  23.  
  24. $icon_files = wp_cache_get('icon_files'); 
  25.  
  26. if ( !is_array($icon_files) ) { 
  27. /** 
  28. * Filters the icon directory path. 
  29. * @since 2.0.0 
  30. * @param string $path Icon directory absolute path. 
  31. */ 
  32. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 
  33.  
  34. /** 
  35. * Filters the icon directory URI. 
  36. * @since 2.0.0 
  37. * @param string $uri Icon directory URI. 
  38. */ 
  39. $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); 
  40.  
  41. /** 
  42. * Filters the list of icon directory URIs. 
  43. * @since 2.5.0 
  44. * @param array $uris List of icon directory URIs. 
  45. */ 
  46. $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); 
  47. $icon_files = array(); 
  48. while ( $dirs ) { 
  49. $keys = array_keys( $dirs ); 
  50. $dir = array_shift( $keys ); 
  51. $uri = array_shift($dirs); 
  52. if ( $dh = opendir($dir) ) { 
  53. while ( false !== $file = readdir($dh) ) { 
  54. $file = basename($file); 
  55. if ( substr($file, 0, 1) == '.' ) 
  56. continue; 
  57. if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { 
  58. if ( is_dir("$dir/$file") ) 
  59. $dirs["$dir/$file"] = "$uri/$file"; 
  60. continue; 
  61. $icon_files["$dir/$file"] = "$uri/$file"; 
  62. closedir($dh); 
  63. wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); 
  64.  
  65. $types = array(); 
  66. // Icon basename - extension = MIME wildcard. 
  67. foreach ( $icon_files as $file => $uri ) 
  68. $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; 
  69.  
  70. if ( ! empty($mime) ) { 
  71. $post_mimes[] = substr($mime, 0, strpos($mime, '/')); 
  72. $post_mimes[] = substr($mime, strpos($mime, '/') + 1); 
  73. $post_mimes[] = str_replace('/', '_', $mime); 
  74.  
  75. $matches = wp_match_mime_types(array_keys($types), $post_mimes); 
  76. $matches['default'] = array('default'); 
  77.  
  78. foreach ( $matches as $match => $wilds ) { 
  79. foreach ( $wilds as $wild ) { 
  80. if ( ! isset( $types[ $wild ] ) ) { 
  81. continue; 
  82.  
  83. $icon = $types[ $wild ]; 
  84. if ( ! is_numeric( $mime ) ) { 
  85. wp_cache_add( "mime_type_icon_$mime", $icon ); 
  86. break 2; 
  87.  
  88. /** 
  89. * Filters the mime type icon. 
  90. * @since 2.1.0 
  91. * @param string $icon Path to the mime type icon. 
  92. * @param string $mime Mime type. 
  93. * @param int $post_id Attachment ID. Will equal 0 if the function passed 
  94. * the mime type. 
  95. */ 
  96. return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );