The get_allowed_mime_types() WordPress PHP function retrieves the list of allowed mime types and file extensions.
Usage
$allowed_mime_types = get_allowed_mime_types($user);
Parameters
- $user (int|WP_User, Optional): User to check. Defaults to current user. Default: null
More information
See WordPress Developer Resources: get_allowed_mime_types()
Examples
Get allowed mime types for the current user
Retrieve the allowed mime types for the current user and display them.
$allowed_mime_types = get_allowed_mime_types(); print_r($allowed_mime_types);
Get allowed mime types for a specific user
Retrieve the allowed mime types for a specific user by providing their user ID.
$user_id = 5; $allowed_mime_types = get_allowed_mime_types($user_id); print_r($allowed_mime_types);
Check if a specific mime type is allowed
Check if a specific mime type (e.g., ‘image/jpeg’) is allowed for the current user.
$mime_type_to_check = 'image/jpeg'; $allowed_mime_types = get_allowed_mime_types(); if (in_array($mime_type_to_check, $allowed_mime_types)) { echo "The mime type is allowed."; } else { echo "The mime type is not allowed."; }
Get allowed mime types for images
Retrieve the allowed mime types for images only and display them.
$allowed_mime_types = get_allowed_mime_types(); $image_mime_types = array_filter($allowed_mime_types, function($mime) { return strpos($mime, 'image/') === 0; }); print_r($image_mime_types);
Check if a specific file extension is allowed
Check if a specific file extension (e.g., ‘jpg’) is allowed for the current user.
$extension_to_check = 'jpg'; $allowed_mime_types = get_allowed_mime_types(); $found = false; foreach ($allowed_mime_types as $extensions => $mime) { if (strpos($extensions, $extension_to_check) !== false) { $found = true; break; } } if ($found) { echo "The file extension is allowed."; } else { echo "The file extension is not allowed."; }