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:
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:
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:
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:
- Use the SVG Support plugin if you don’t want to mess around with code.
- 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!
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?
Its close but it just doesnt work. Tested with storefront and child theme.
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.
create a child theme
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.
Method 2 didn’t work. Thumbs down for you.
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)!