wp_match_mime_types

Check a MIME-Type against a list.

Description

(array) wp_match_mime_types( (string|array) $wildcard_mime_types, (string|array) $real_mime_types ); 

If the wildcard_mime_types parameter is a string, it must be comma separated list. If the real_mime_types is a string, it is also comma separated to create the list.

Returns (array)

array(wildcard=>array(real types)).

Parameters (2)

0. $wildcard_mime_types (string|array)
Mime types, e.g. audio/mpeg or image (same as image/**) or flash (same as *flash*).
1. $real_mime_types (string|array)
Real post mime type values.

Usage

  1. if ( !function_exists( 'wp_match_mime_types' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Mime types, e.g. audio/mpeg or image (same as image/**) 
  5. // or flash (same as *flash*). 
  6. $wildcard_mime_types = null; 
  7.  
  8. // Real post mime type values. 
  9. $real_mime_types = null; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = wp_match_mime_types($wildcard_mime_types, $real_mime_types); 
  13.  

Defined (1)

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

/wp-includes/post.php  
  1. function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { 
  2. $matches = array(); 
  3. if ( is_string( $wildcard_mime_types ) ) { 
  4. $wildcard_mime_types = array_map( 'trim', explode( ', ', $wildcard_mime_types ) ); 
  5. if ( is_string( $real_mime_types ) ) { 
  6. $real_mime_types = array_map( 'trim', explode( ', ', $real_mime_types ) ); 
  7.  
  8. $patternses = array(); 
  9. $wild = '[-._a-z0-9]*'; 
  10.  
  11. foreach ( (array) $wildcard_mime_types as $type ) { 
  12. $mimes = array_map( 'trim', explode( ', ', $type ) ); 
  13. foreach ( $mimes as $mime ) { 
  14. $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); 
  15. $patternses[][$type] = "^$regex$"; 
  16. if ( false === strpos( $mime, '/' ) ) { 
  17. $patternses[][$type] = "^$regex/"; 
  18. $patternses[][$type] = $regex; 
  19. asort( $patternses ); 
  20.  
  21. foreach ( $patternses as $patterns ) { 
  22. foreach ( $patterns as $type => $pattern ) { 
  23. foreach ( (array) $real_mime_types as $real ) { 
  24. if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) { 
  25. $matches[$type][] = $real; 
  26. return $matches;