Support SVG in WordPress dashboard media

To add support for SVG (Scalable Vector Graphics) images in the WordPress dashboard using the functions.php file of your theme or a custom plugin, you can use the wp_check_filetype_and_ext filter to allow SVG file uploads.

Open your theme’s functions.php file or create a custom plugin:

Appearance > Theme Editor

// Allow SVG file uploads
function allow_svg_upload($data, $file, $filename, $mimes) {
  $filetype = wp_check_filetype_and_ext($filename, $filename, $mimes);

  return [
    'ext'       => $filetype['ext'],
    'type'      => $filetype['type'],
    'proper_filename' => $data['proper_filename']
  ];
}
add_filter('wp_check_filetype_and_ext', 'allow_svg_upload', 10, 4);

// Allow SVG file upload in WordPress Media Library
function allow_svg_mime_types($mimes) {
  $mimes['svg'] = 'image/svg+xml';
  $mimes['svgz'] = 'image/svg+xml';

  return $mimes;
}
add_filter('upload_mimes', 'allow_svg_mime_types');

Save your changes.

Leave a comment

Your email address will not be published. Required fields are marked *