If you’ve tried simply uploading an SVG to WordPress through the Media Uploader, you may have had a few issues.
Either it gave you an error and wouldn’t let you upload the file or it allowed you to upload the .svg file but just wouldn’t display it. Either way, here’s two simple steps for enabling SVG images in WordPress easily.
Note: You’ll need to be able to edit your theme’s (or child theme’s) functions.php
file and the root .htaccess
file for this to work.
Add SVG MIME Types to functions.php
function wpcontent_svg_mime_type( $mimes = array() ) {
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'wpcontent_svg_mime_type' );
You should replace wpcontent_
with your own namespace. This function simply adds the SVG and SVGZ (compressed SVG) to the allowed upload file types in WordPress and hooks into the upload_mimes() WordPress function.
Add SVG Mime Types to .htaccess
So, open your root .htaccess
file and add the following after the line, #End WordPress
.
# Add SVG Mime Types
AddType image/svg+xml svg
AddType image/svg+xml svgz
Save the file and you’re done! You can now save SVGs from Illustrator or Inkscape and use them on your WordPress site.
Source here