To upload SVG files (Scalable Vector Graphics) to WordPress you don’t need any additional plugin, simply add the following code to your theme’s functions.php file:
/* Upload .svg images to WordPress */
add_filter('wp_check_filetype_and_ext', function ($data, $file, $filename, $mimes) {
if (!$data['type']) {
$wp_filetype = wp_check_filetype($filename, $mimes);
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
$proper_filename = $filename;
if ($type && 0 === strpos($type, 'image/') && $ext !== 'svg') {
$ext = $type = false;
}
$data['ext'] = $ext;
$data['type'] = $type;
$data['proper_filename'] = $proper_filename;
}
return $data;
}, 10, 4);
add_filter('upload_mimes', function ($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
add_action('admin_head', function () {
echo '';
});
/* Display .svg on front end */
img[src$=".svg"] { width: auto; height: auto; }
To be able to see uploaded SVG files in the media library you can also add the following code:
function webp_is_displayable($result, $path) {
if ($result === false) {
$displayable_image_types = array( IMAGETYPE_svg );
$info = @getimagesize( $path );
if (empty($info)) {
$result = false;
} elseif (!in_array($info[2], $displayable_image_types)) {
$result = false;
} else {
$result = true;
}
}
return $result;
}
add_filter('file_is_displayable_image', 'svg_is_displayable', 10, 2);