wp_match_mime_types() WordPress Function

The wp_match_mime_types() function is used to match a given MIME type against a list of MIME types.

wp_match_mime_types( string|string[] $wildcard_mime_types, string|string[] $real_mime_types ) #

Check 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.


Top ↑

Parameters

$wildcard_mime_types

(string|string[])(Required)Mime types, e.g. audio/mpeg or image (same as image/*) or flash (same as *flash*).

$real_mime_types

(string|string[])(Required)Real post mime type values.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/post.php

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 ( false === strpos( $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 ] ) || false === array_search( $real, $matches[ $type ], true ) )
				) {
					$matches[ $type ][] = $real;
				}
			}
		}
	}

	return $matches;
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More