Adding this snippet to the functions.php of your wordpress theme will add support for uploading SVG inside Wordpress Media Uploader. It is important to consider risk when uploading SVG as it is an XML file. Keep security in mind and don’t allow just anyone to upload SVG. You also may want to consider running the Scalable Vector Graphics (SVG) plugin if you have any concerns.

There are many use cases for allowing users to upload files from the frontend, but the trick is making sure the file is uploaded and saved in the WordPress media library correctly. You can easily use standard PHP functions to handle the upload and then store the file wherever you like, but then you won’t be able to take advantage of the attachment system that WordPress uses. An image that is uploaded correctly, for example, will have all the various sizes available as well as the meta data, such as title, description and caption. You will also be able to edit all of this from the media library in your WordPress dashboard.

Adding this snippet to the functions.php of your wordpress theme will allow you to restrict uploads to an array of specified mime types only. This sample only allows jpg and gif images, adding a new line for png eg: ‘png’ => ‘image/png’, would then allow png images.

About wpsnipp

WPSNIPP Was founded in 2010 by Kevin Chard and is a free resource dedicated to WordPress covering a wide range of topics. With over 640+ WordPress snippets, we also have tutorials, plugins, gallery and deals to enhance all aspects of your WordPress theme or blog.