add SVG to WordPress

WordPress supports multiple image formats out of the box, including .jpg.jpeg.png, and .gif. However, it doesn’t enable you to use Scalable Vector Graphics (SVGs), which is one of the most flexible image types out there. Fortunately, learning how to add SVG to WordPress is quite simple.

I’ll show you tow ways to enable this image format. After that, you’ll be able to use SVGs within your content as regular images, logos, and more. What’s more, WordPress works flawlessly with SVGs once you’ve enabled the file type.

How to add SVG to WordPress (two easy methods)

As we mentioned earlier, WordPress doesn’t support SVGs out of the box. However, you can either enable this feature manually or use a plugin to do it. Let’s start with the latter method because it’s the simplest.

Method 1: Use the SVG Support plugin

If you’re looking for the fastest way to add SVG to WordPress, this is it. We’re going to use the SVG Support plugin, which enables this particular image format and adds support for it to your media library:

The process is simple. You just need to install and activate the plugin as usual, and then you’ll be able to add SVGs to your WordPress site.

WordPress now requires us to have the <xml> tag in our SVG files before uploading. Please open your SVG file in any code editor (such as sublime text) and add the following to the very first line of your SVG file and save, so that you don’t encounter security errors:

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

However, there are two more settings you might want to change depending on your needs. First off, let’s go to the Settings → SVG Support tab:

How to add SVG to WordPress using SVG Support plugin

Inside, you’ll find two options. The first turns on the plugin’s Advanced Mode, which lets you target your SVGs with CSS. If you don’t want to animate your SVGs, then you can skip this option.

Second, you can also restrict the ability to upload SVGs to administrators only by enabling the Restrict to Administrators? feature. That one’s up to you!

Method 2: Modify your site’s functions.php file

Every WordPress website has its own functions.php file. This essential component includes important functions, classes, and filters. It’s also your ticket to adding SVG support to WordPress through a few lines of code.

To reach this file, you’ll need to access your website via FTP. If you don’t have a client, we recommend using FileZilla. Once you’ve found your FTP credentials and accessed your site, you’ll want to head to your root folder, which is usually either called public_html or named after your site:

The WordPress root folder.

Now, enter the wp-includes folder and look for the functions.php file within. It’s important to note that this is the parent file, while there are also individual functions.php files for each of your themes:

The wp-includes folder.

For this example, we’ll add the code to the parent file. However, you may find the changes are lost when WordPress is updated, so feel free to alternatively add it to your theme-specific functions.php file depending on the approach you’re more comfortable with.

(Editor’s note: Doing this in your theme’s functions file is actually the recommended approach.)

Access the functions.php file now by right-clicking on it and choosing the View/Edit option. This will open it using your default text editor. Now, scroll to the bottom and paste this code snippet there:

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');

Save your changes to the file and close it. Then you can return to your dashboard and test your new feature by uploading an SVG file to your media library, which should work without a hitch!

Conclusion

SVGs are one of the most interesting image types you can use on your website. Not only does their scalability make them a good option for responsive sites, you can also animate them so they can substitute for GIFs in certain situations. Plus, enabling them in WordPress is remarkably simple.

Just pick one of these two methods to add SVG to WordPress, and you’ll be able to play around with the file type to your heart’s content:

  1. Use the SVG Support plugin if you don’t want to mess around with code.
  2. Modify your functions.php file to add support for SVG if you don’t want to install an extra plugin.

Do you have any questions about how to add SVG to WordPress? Ask away in the comments section below!

Free guide

4 Essential Steps to Speed Up
Your WordPress Website

Follow the simple steps in our 4-part mini series
and reduce your loading times by 50-80%. 🚀

Free Access

7 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Kevin Geary
May 22, 2018 11:40 pm

Unfortunately, Facebook open graph doesn’t support SVG, so if you use SVG in featured images, it’ll break images on most social platforms. Any workarounds?

Isaac Joseph Horton
March 26, 2018 7:49 pm

Its close but it just doesnt work. Tested with storefront and child theme.

Nataša
March 26, 2018 12:23 pm

I don’t know if it’s a good idea to edit the theme’s function.php because when the theme is updated, that edit goes out the window.

Chris Pitcher
April 25, 2018 12:07 pm
Reply to  Nataša

create a child theme

Nataša
April 25, 2018 1:24 pm
Reply to  Chris Pitcher

Okay. I’d suggest modifying “Editor’s note: Doing this in your theme’s functions file is actually the recommended approach” to reflect making these changes in a child theme.

allan
March 22, 2018 6:58 pm

Method 2 didn’t work. Thumbs down for you.

Amine Laissoub
December 23, 2017 6:50 pm

Modify functions.php file to add support for SVG doesn’t allow you to preview the svg file on Media Library.

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!