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
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.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$wildcard_mime_typesstring|string[]- Mime types, e.g.
audio/mpeg,image(same asimage/*), orflash(same as*flash*). $real_mime_typesstring|string[]- Real post mime type values.
Return value
array- array(wildcard=>array(real types)).
Performance profile
How much work a call to wp_match_mime_types() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 17–37
- Plugin surface
- None
- Called by
- 7
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 101.
Nothing here hands control to plugin code.
7 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_match_mime_types() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($wildcard_mime_types) && !is_string($real_mime_types) | 17–19 | asort() |
| always | 26–28 | explode(), array_map(), asort() |
is_string($wildcard_mime_types) && is_string($real_mime_types) | 35–37 | explode(), array_map(), explode(), array_map(), asort() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 101 | 17–37 | 16 | |
| 8.5 | 101 | 17–37 | 16 | |
| 8.4 | 101 | 17–37 | 16 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 116 | 17–37 | 16 | |
| 8.2 | 116 | 17–37 | 16 | |
| 8.1 | 116 | 17–37 | 16 | |
| 7.4 | 116 | 17–37 | 16 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 7
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Image_Header::step_2_manage_upload()Uploads the file to be cropped in the second step.
- WP_Media_List_Table::get_views()Gets an array of links for the available views on this table.
- get_media_item()Retrieves HTML form for modifying the image attachment.
- media_upload_library_form()Outputs the legacy media upload form for the media library.
- wp_ajax_upload_attachment()Handles uploading attachments via AJAX.
- wp_mime_type_icon()Retrieves the icon for a MIME type or attachment.
Source code
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;}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 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.