How to Add SVG Images to the WordPress Media Library

aniaromanska

Written by Ania Romańska

Published on . Last edited on .

WordPress Media Library upload screen with SVG files and a pink SVG upload icon

TL;DR

WordPress core (checked in version 7.1) still doesn't allow SVG uploads by default.

Divi 5.18 allows SVG uploads for users who can manage theme options (administrators) and sanitizes each SVG on upload, removing scripts and event attributes like onload.

Divi 5.18 removes SVG from the allowed file types for other roles, such as editors and authors, even if a code snippet adds it.

Divi 5's SVG module shows pasted SVG code or an uploaded SVG file, and the Icon Manager accepts custom SVG icons.

Without Divi 5, use a sanitizing plugin such as Safe SVG or SVG Support instead of the plain upload_mimes snippet.

Updated for Divi 5: WordPress still blocks SVG uploads by default, but Divi 5 now lets administrators upload SVG files and cleans them on upload. I tested it with Divi 5.18 and rewrote this post, because the old code snippet is no longer needed with Divi 5 (and it never checked the files for anything harmful).

Can you upload SVG images to WordPress?

Not out of the box: WordPress (I checked version 7.1) still doesn’t include SVG in its list of allowed file types. That’s on purpose, because an SVG file is code and can contain scripts, so you need something that allows SVG uploads and ideally also sanitizes the files.

SVG images are perfect for logos and icons: they stay sharp at any size and are usually tiny. So it’s worth enabling them, just in a safe way.

How to upload SVG images in Divi 5

If you’re an administrator, simply upload the SVG to the Media Library: on my Divi 5.18 test site it worked without any plugin or code. Divi 5 adds SVG to the allowed file types for users who can manage theme options (administrators), and it sanitizes every SVG when it’s uploaded.

To check it, I uploaded a test SVG with a <script> tag and an onload attribute inside. Divi removed both before the file was saved, and the circle in the image stayed untouched.

A few things to know:

  • Only administrators. Divi 5 removes SVG from the allowed file types for other roles (editors, authors), on purpose, even when a code snippet adds it. I tested this with the snippet from the original version of this post.
  • The SVG module. Divi 5 has an SVG module that can show pasted SVG code or an uploaded SVG file (its Source Type setting), and the Icon Manager lets you upload your own SVG icons.
  • Elegant Themes’ advice. Their help article How to Upload SVG and JSON Files still recommends a plugin for SVG uploads outside the Icon Manager. If uploading doesn’t work on your site, follow it.

How to upload SVG images without Divi 5

Use a plugin that allows SVG uploads and sanitizes the files, such as Safe SVG or SVG Support. That’s the safest way on a Divi 4 site or any other theme.

One thing to keep in mind: with Divi 5 active, SVG uploads stay limited to administrators even with one of these plugins, because Divi removes SVG from the other roles’ allowed file types (that’s how Divi 5.18’s code works; I didn’t test every plugin).

The old functions.php snippet (and a safer version)

The snippet from the first version of this post only adds SVG to the allowed file types. It doesn’t check what’s inside the file, so anyone who can upload media could upload an SVG with harmful code. Here is the original:

// Allow SVG uploads
function allow_svgimg_types($mimes) {
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter('upload_mimes', 'allow_svgimg_types');

If you can’t use a plugin and you only upload SVG files you made or trust, at least limit it to administrators:

// Allow SVG uploads for administrators only (no sanitizing!)
function dl_allow_svg_uploads( $mimes ) {
    if ( current_user_can( 'manage_options' ) ) {
        $mimes['svg'] = 'image/svg+xml';
    }
    return $mimes;
}
add_filter( 'upload_mimes', 'dl_allow_svg_uploads' );

Remember to use a child theme

If you’re using Divi or any other pre-made theme, don’t forget to make a child theme first and add the code to the functions.php file in your child theme (or use a code snippets plugin). If you make changes in the parent theme’s files, all your changes will be gone when you update the theme.

Tips for using SVG images safely

Only upload SVG files from sources you trust, keep them small and give them clear names. A few habits that help:

  • Clean up the file first. Icons exported from design tools often contain extra code. A free tool like SVGOMG makes them smaller and cleaner.
  • Name your files before you upload them. SVG files don’t always get a nice preview, so a clear name (like icon-heart-pink.svg) helps you find the right one later.
  • Don’t give upload rights to people you don’t trust. Sanitizing helps a lot, but the safest SVG is one that comes from you.

Have questions about using SVG images in Divi? Ask in my Divi Lovers Facebook group.

This article has been tagged with: Divi Fixes

Tags

WordPress Media Library upload screen with SVG files and a pink SVG upload icon

Table of Contents

Popular questions

Is it safe to upload SVG files to WordPress?

Only if the files are sanitized or come from a source you trust. An SVG is code and can contain scripts, which is why WordPress blocks SVG uploads by default. Use Divi 5's built-in sanitizing (for administrators) or a plugin like Safe SVG.

Do I need a plugin to upload SVG files in Divi 5?

On my Divi 5.18 test site, administrators could upload SVG files to the Media Library without a plugin, and Divi sanitized them on upload. Elegant Themes still recommends SVG Support or Safe SVG for uploads outside the Icon Manager, so install one if your upload is blocked.

Why can't my editor upload SVG files in Divi 5?

Divi 5 allows SVG uploads only for users who can manage theme options, which by default means administrators. It removes SVG from the allowed file types for every other role, even when upload_mimes code adds it.

Where do I add the SVG upload snippet?

Add it to your child theme's functions.php file or to a code snippets plugin, never to the parent theme, because theme updates overwrite it. With Divi 5 you usually don't need it at all.

About the Author

Ania Romańska

I'm a Divi expert from Poland, with over 10 years building the templates and courses that make up Divi Lover. I'm also a mom of five, which taught me a thing or two about explaining things clearly the first time.

Hey there!Sorry to interrupt,but this one's important!

Divi Lover is turning 10!
Let’s celebrate with 40% off everything.

Ten years of Divi templates, plugins and tips, and a brand-new website to celebrate it. To say thank you, every product in the shop is 40% off until Cyber Monday.

Your code

Paste it at checkout. Valid until November 30.