I have previously managed to utilise this block of code within my functions.php file to enable and create thumbnails for SVGs that are uploaded to my site:

    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_action('upload_mimes', 'add_file_types_to_uploads');

    function common_svg_media_thumbnails($response, $attachment, $meta){
        if($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && class_exists('SimpleXMLElement'))
            try {
                $path = get_attached_file($attachment->ID);
                    $svg = new SimpleXMLElement(@file_get_contents($path));
                    $src = $response['url'];
                    $width = (int) $svg['width'];
                    $height = (int) $svg['height'];

                    //media gallery
                    $response['image'] = compact( 'src', 'width', 'height' );
                    $response['thumb'] = compact( 'src', 'width', 'height' );

                    //media single
                    $response['sizes']['full'] = array(
                        'height'        => $height,
                        'width'         => $width,
                        'url'           => $src,
                        'orientation'   => $height > $width ? 'portrait' : 'landscape'
            catch(Exception $e){}

        return $response;
    add_filter('wp_prepare_attachment_for_js', 'common_svg_media_thumbnails', 10, 3);

I am now trying to copy this code over to a new site but I still receive the error:

"logo.svg” has failed to upload.
Sorry, this file type is not permitted for security reasons.

This site is located within a subdirectory of a non-WordPress website, like:


Am I missing a step to get this working?

~ Please do not reference plug-ins.

~ Please help me get this code snippet that has been previously proven to work on a different site; working on this new site.

Thanks, Jason.

  • 2
    Keep in mind that the code in this question is dangerous and will allow the upload of malicious and compromised SVG's. SVG's are not images, they're documents, and can contain arbitrary HTML and javascript. A plugin such as SafeSVG will sanitise SVGs on upload so that they can be safely added to a site. Keep in mind that plugin recommendations are offtopic on this site though, hence why I leave this as a comment. Perhaps you can provide some context for why? That would help people understand rather than just a blanket ban that invites questions, or, make alternatives clearer
    – Tom J Nowell
    Aug 25, 2020 at 11:09
  • 1
    Also, you should remove the @ on the file_exists, @ does not fix issues, it simply silences them, the problem still happens, all it does is redirect the error message to trash.
    – Tom J Nowell
    Aug 25, 2020 at 11:10
  • Thank you for highlighting that, that is enough of a reason to me to switch to using a plug-in. Appreciate the help! Aug 25, 2020 at 11:13
  • 1
    The plugin is mostly a wrapper around a library. For this Q, I suspect the problem is the mimetype, but I'm unsure of the exact specifics to write an answer
    – Tom J Nowell
    Aug 25, 2020 at 11:18
  • Same thinking here, also not sure how though. Aug 25, 2020 at 11:19

1 Answer 1

function cc_mime_types($mimes) {
  $mimes['svg'] = 'image/svg+xml'; 
  return $mimes;
add_filter('upload_mimes', 'cc_mime_types'); 

add in functions.php in your active theme


add in wp-config.php

  • 2
    To be clear, this is two answers, you don't need to do both. If you allow unfiltered uploads, you are effectively turning off the file type validation.
    – JDandChips
    Jan 10, 2022 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.