skip to Main Content

I have the new 5.8.1 wordpress, but svg in the media uploader aren’t working anymore.

I used this code to integrate svgs:

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

Why is it not working?

2

Answers


  1. You first need to mention the file type and extension.
    here is my code for that !

    // Wp v4.7.1 and higher
    add_filter( 'wp_check_filetype_and_ext', function($data, $file, $filename, $mimes) {
      $filetype = wp_check_filetype( $filename, $mimes );
      return [
          'ext'             => $filetype['ext'],
          'type'            => $filetype['type'],
          'proper_filename' => $data['proper_filename']
      ];
    
    }, 10, 4 );
    
    function cc_mime_types( $mimes ){
      $mimes['svg'] = 'image/svg+xml';
      return $mimes;
    }
    add_filter( 'upload_mimes', 'cc_mime_types' );
    
    function fix_svg() {
      echo '<style type="text/css">
            .attachment-266x266, .thumbnail img {
                 width: 100% !important;
                 height: auto !important;
            }
            </style>';
    }
    add_action( 'admin_head', 'fix_svg' );
    
    Login or Signup to reply.
  2. After WP 4.7 you dont need to use svg+xml with your current code OR you have to specify file type as example below

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

    You can check alot of different solutions here – https://wordpress.stackexchange.com/questions/313951/how-to-upload-svg-in-wordpress-4-9-8

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search