wppaste
WordPress

wp_match_mime_types( string|string[] $wildcard_mime_types, string|string[] $real_mime_types ): array

Since
2.5.0
Source
wp-includes/post.php:3712
Checks a MIME-Type against a list.

Description

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.

Parameters

$wildcard_mime_typesstring|string[]
Mime types, e.g. audio/mpeg, image (same as image/*), or flash (same as *flash*).
$real_mime_typesstring|string[]
Real post mime type values.

Return

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

Uses · 1

  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.

Used by · 7

Source

function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {	$matches = array();	if ( is_string( $wildcard_mime_types ) ) {		$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );	}	if ( is_string( $real_mime_types ) ) {		$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );	} 	$patternses = array();	$wild       = '[-._a-z0-9]*'; 	foreach ( (array) $wildcard_mime_types as $type ) {		$mimes = array_map( 'trim', explode( ',', $type ) );		foreach ( $mimes as $mime ) {			$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); 			$patternses[][ $type ] = "^$regex$"; 			if ( ! str_contains( $mime, '/' ) ) {				$patternses[][ $type ] = "^$regex/";				$patternses[][ $type ] = $regex;			}		}	}	asort( $patternses ); 	foreach ( $patternses as $patterns ) {		foreach ( $patterns as $type => $pattern ) {			foreach ( (array) $real_mime_types as $real ) {				if ( preg_match( "#$pattern#", $real )					&& ( empty( $matches[ $type ] ) || ! in_array( $real, $matches[ $type ], true ) )				) {					$matches[ $type ][] = $real;				}			}		}	} 	return $matches;}

History

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.