How to enable SVG uploads into WordPress media

By default the WordPress media section does not allow SVG’s. This is due to security, although it’s debateable whether this restriction is really necessary. As it becomes ever more common to use SVG’s, there are many situations where we would like to store them in WP media attachments.

// Enable SVG uploads to media.
add_filter( 'upload_mimes', function ( $types ) {
  $types['svg']  = 'image/svg+xml';
  return $types;
});

The snippet above uses the “upload_mimes” filter with a callback function to add SVG file types to the accepted mime type list. This can be placed in your theme functions.php file or can be added in a custom plugin.

In case you’re wondering can I adapt this snippet to work for JSON files or other file types that are usually blocked, yes you can! The tricky part is finding the correct mimetype string (in this example the image/svg+xml part, and certain types of files like JSON may require a different mimetype, or the mimetype may fail due to some additional server restriction beyond the control of WordPress or PHP.

Casey Milne

Founder of SaberWP. PHP Programmer for the past 20-years.