How to Add Additional File Types to Be Uploaded in Wordpress Plugin

Being able to manage all the files we upload in WordPress in one place (the media library) is something very useful. Some say the media library could be improved—some requested features are even so missing, like file tagging, sorting by topic or by file extension, or searching for duplicates—, but its usefulness is clear when reusing or sharing files between our contents without having to upload them more than once.

Mayhap y'all didn't know that, but WordPress only allows file uploading into the media library if such files run across certain conditions. Specifically, you tin only upload the following file types by default in a standard WordPress installation:

  • Images
    • .jpg
    • .jpeg
    • .png
    • .gif
    • .ico
  • Documents
    • .pdf (Portable Document Format; Adobe Acrobat)
    • .doc, .docx (Microsoft Word Certificate)
    • .ppt, .pptx, .pps, .ppsx (Microsoft PowerPoint Presentation)
    • .odt (OpenDocument Text Document)
    • .xls, .xlsx (Microsoft Excel Document)
    • .psd (Adobe Photoshop Document)
  • Audio
    • .mp3
    • .m4a
    • .ogg
    • .wav
  • Video
    • .mp4, .m4v (MPEG-4)
    • .mov (QuickTime)
    • .wmv (Windows Media Video)
    • .avi
    • .mpg
    • .ogv (Ogg)
    • .3gp (3GPP)
    • .3g2 (3GPP2)

However, it's non ever possible to upload all these file types—some hosting providers limit the set up of valid file types and even the maximum file size you can upload in your installation. If yous take problems uploading some permitted file types, contact your provider first to become an explanation.

When trying to upload a .SVG image in WordPress you get an error.
When trying to upload a .SVG image in WordPress you lot get an mistake.

Recently we received a comment asking u.s.a. how to expand the list by adding more file types. As surely in that location are more than people interested in learning how to do this, in this mail service I explain the solution.

Modiyingwp-config.php

If you lot do not want to limit the file types and thus upload any file into the media library, the easiest way to follow is to add the following line in the wp-config.php file:

ascertain( 'ALLOW_UNFILTERED_UPLOADS', true );

The wp-config.php file allows us to modify the default beliefs of WordPress. Yous'll find information technology in the root of your server. Using the above argument we are allowing all administrators to upload any file type. Note that this can be dangerous if you practise not trust too much your administrators and what they can do. Exist careful when adding this statement or you lot might end up regretting your decision ?

Using the upload_mimes filter

If y'all desire to solve the trouble of uploading additional file types to WordPress, only being selective and merely allowing a controlled subset of file types, the best you lot tin do is use the upload_mimes filter. As always, yous accept all the information in the Codex.

This filter is super easy to utilize. You only need to add a piece of code similar to the post-obit in the functions.php file of your theme or inside a new plugin of your own:

Y'all can ignore the first line (<?php), which merely opens a cake of PHP code. Line 2 indicates that the part my_mime_types will be executed with the upload_mimes filter. Lines 3 to 10 define this function. my_mime_types has one single parameter ($mime_types)—the list of allowed file types.

If you lot want to allow SVG and JSON files in the media library, just add lines 4 and five, respectively. This is how it works: to add a new file type, y'all simply accept to create a new entry in the $mime_types list with the file extension and mime type. To find the proper mime blazon of an extension go to the official list of media types that the IANA (Internet Assigned Numbers Authority) defines.

On the other hand, you tin can also disable certain file types. You can prohibit the file types you want by but unsetting their entries in the $mime_types array, as I did in lines 7 and 8, where I disabled Microsoft Excel file uploading.

Summary

As y'all tin can see hither, modifying the default behavior of WordPress to have greater control over the file types you permit in your installation is very simple. And best of all, yous don't need to install a complex plugin for this. With just a few lines of PHP code or even editing the wp-config.php file yous'll get what you want.

Now it's your plow. Explain us which file types you miss in WordPress and your experience using the solutions I have proposed in this article.

Featured image by Andrew Pons.

scottforsel1979.blogspot.com

Source: https://neliosoftware.com/blog/how-to-upload-additional-file-types-in-wordpress/

0 Response to "How to Add Additional File Types to Be Uploaded in Wordpress Plugin"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel