Using the get_allowed_mime_types function in WordPress

The get_allowed_mime_types function in WordPress is a built-in system feature that retrieves a list of all the MIME types which are allowed to be uploaded to the WordPress site. MIME types are a way of identifying files on the internet according to their nature and format. For instance, using image/jpeg for JPEG images, and text/html for HTML files.

This function is a part of WordPress’ content management system and is used to control the types of files that users can upload to the platform. By default, WordPress allows a certain set of file types to ensure the security and integrity of the site. The get_allowed_mime_types function fetches this list of permitted file types.

The function can be used to check if a specific file type is allowed before attempting to upload it, providing a way to handle the upload process more efficiently and securely. This can be particularly relevant in scenarios where there is a need to restrict or allow specific file types based on security requirements or the nature of the website.

Parameters Accepted by the get_allowed_mime_types Function

The get_allowed_mime_types function in WordPress accepts a single parameter:

  • $user (intWP_User): This is an optional parameter. By default, its value is null. The purpose of this parameter is to specify the user to be checked. If not explicitly defined, the function will default to the current user.

Return Value of the get_allowed_mime_types Function

When called, the get_allowed_mime_types function returns an array of string values. These strings represent mime types. Each mime type in the array is associated with a file extension regex that corresponds to that specific mime type.

If the function does not accept any parameters, it will be explicitly stated in a concise manner.

Examples

1. How to display all allowed mime types

This code snippet will display all the allowed mime types in WordPress.

$mime_types = get_allowed_mime_types();
foreach($mime_types as $type => $mime) {
 echo '<p>' . $type . ' => ' . $mime . '</p>';
}

In this first example, the get_allowed_mime_types function retrieves all the allowed mime types and the foreach loop displays each mime type.

2. How to check if a specific mime type is allowed

This code snippet will check if a specific mime type, in this case ‘image/jpeg’, is allowed.

$mime_types = get_allowed_mime_types();
if (in_array('image/jpeg', $mime_types)) {
 echo '<p>Image/jpeg is allowed.</p>';
} else {
 echo '<p>Image/jpeg is not allowed.</p>';
}

Conclusion

The get_allowed_mime_types function in WordPress plays a significant role in controlling the types of files that can be uploaded to a site. It operates by returning an array of mime types which WordPress allows to be uploaded. This function is primarily used in scenarios where there is a need to limit or control the file types that users can upload to a WordPress site, enhancing security and preventing unauthorized or potentially harmful file uploads.

Related WordPress Functions