Back to all posts

How To Allow SVG Images into WordPress.


Add the below code in your theme functions.php file

<?php
//Function
function cc_mime_types($mimes)
{
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}

function fix_svg()
{
  echo '<style type="text/css">
          .attachment-266x266, .thumbnail img {
               width: 100% !important;
               height: auto !important;
          }
          </style>';
}

//Actions
//  this below code is function inside action
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);

add_filter('upload_mimes', 'cc_mime_types');
add_action('admin_head', 'fix_svg');

?>