How to Display a List of Allowed Mime Types using get_allowed_mime_types in WordPress

Home » Snippets » How to Display a List of Allowed Mime Types using get_allowed_mime_types in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you a WordPress developer looking to display a list of allowed mime types in your website? WordPress has a built-in function called get_allowed_mime_types that allows you to obtain a list of all the allowed file types on your site. In this article, we will guide you through the process of using this function to retrieve and display the list of allowed mime types in your WordPress website.

					function wpturbo_display_allowed_mime_types() {
    $mime_types = get_allowed_mime_types();
    if ($mime_types) {
        echo "<ul>";
        foreach ($mime_types as $mime_type => $label) {
            echo "<li>{$mime_type} - {$label}</li>";
        }
        echo "</ul>";
    }
}
add_action('admin_notices', 'wpturbo_display_allowed_mime_types');
				

The code snippet provided helps in displaying a list of allowed mime types in WordPress. This functionality can be useful for developers or users who want to have a clear understanding of the file types that are allowed to be uploaded on their WordPress site.

In the first line of the wpturbo_display_allowed_mime_types() function, we call the get_allowed_mime_types() function. This WordPress core function retrieves an array of allowed mime types and their corresponding labels. This array contains a list of default allowed mime types, as well as any custom mime types that may have been added or modified.

Next, we check if there are any mime types available in the mime_types array. If the array is not empty, we proceed with the code inside the if statement.

We start by echoing an opening ul tag to indicate the start of an unordered list. This will be used to display the list of allowed mime types.

Using a foreach loop, we iterate through each key-value pair in the mime_types array. The key represents the mime type (such as "image/jpeg" or "application/pdf") and the corresponding label represents a human-readable description of the mime type.

Inside the loop, we echo an li tag for each mime type and its label. We use variable interpolation to output the mime type and label values within the HTML.

After the loop completes, we echo a closing ul tag to indicate the end of the unordered list.

Finally, we hook the wpturbo_display_allowed_mime_types() function to the admin_notices action. This ensures that the list of allowed mime types is displayed in the admin dashboard whenever WordPress renders its admin notices.

By simply adding this code snippet to the theme’s functions.php file or a custom plugin file, the list of allowed mime types will automatically be displayed in the WordPress admin dashboard through the use of the admin_notices action hook.

Register an account to save your snippets or go Pro to get more features.