Enabling SVG Uploads in WordPress

SVGs have advantages over other image types. They scale without losing quality and can be styled and animated using css and javascript. Like all advantages, these come with a cost – SVGs can contain malicious code. This is why WordPress does not allow SVG uploads by default, and you shouldn’t either unless you’re sure it won’t be a problem for your use-case.

So for those I didn’t just scare away, here’s how to enable SVG uploads in WordPress.

Locate the functions.php file in your theme, and add this code to the bottom of the file. This will add SVGs to the accepted list of file types.

function 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', 'add_file_types_to_uploads');

Now if you try to upload an SVG you will likely still get an error stating your image is not an acceptable file type. This is because all SVGs you upload must include the xml declaration as the first line in your SVG file.

<?xml version="1.0" encoding="utf-8"?>

After adding that line you should be able to upload SVGs.

Leave a Reply