How to Add File Types for Media Uploads in WordPress

Home » Snippets » How to Add File Types for Media Uploads 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

As a WordPress user, you might have noticed that the media library only allows you to upload certain file types. This can be frustrating if you need to upload a file that is not included in the default list. However, there is a solution: by adding a few lines of code to your functions.php file, you can easily add new file types to the media library. In this article, we’ll show you how to do just that. So let’s get started!

					function wpturbo_add_file_types_to_uploads( $file_types ) {
    $new_filetypes = array();
    $new_filetypes['svg'] = 'image/svg+xml';
    $file_types = array_merge( $file_types, $new_filetypes );
    return $file_types;
}
add_filter( 'upload_mimes', 'wpturbo_add_file_types_to_uploads' );
				

Let’s explore how to add file types for WordPress media library by creating a custom function that modifies the upload_mimes filter.

The function wpturbo_add_file_types_to_uploads takes in an array of current file types that WordPress supports and returns an array of new file types to be supported.

In this example, we are adding support for SVG files to be uploaded. So, we create a new array called $new_filetypes and add the SVG file type to it:

$new_filetypes['svg'] = 'image/svg+xml';

Next, we merge the new file types with the existing file types array using the array_merge function:

$file_types = array_merge( $file_types, $new_filetypes );

Finally, we return the modified list of file types array by returning the $file_types variable from the function:

return $file_types;

Now, we hook this function into the upload_mimes filter using the add_filter function:

add_filter( 'upload_mimes', 'wpturbo_add_file_types_to_uploads' );

This code will modify the allowed file types in WordPress media library and allow SVG files to be uploaded. You can add additional file types in a similar way by modifying the $new_filetypes array.

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