In 2024, the WordPress Media Library does not permit uploading SVG file types by default.
Trying to upload an SVG file to the WordPress Media Library results in the warning, “Sorry, this file type is not permitted for security reasons.”
Therefore, to change your site’s functionality and permit SVG upload, you’ll need to edit your theme or child theme’s function.php file.
Function for adding SVG support in WordPress
Below is a simple function enabling SVG support in the WordPress media library. Add functions to your site’s function.php file.
function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
$mimes['svg'] = 'image/svg';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
2024 SVG Update
In 2024 using the Block Editor, I needed to change the snippet for SVGs to the following.
add_filter( 'wp_check_filetype_and_ext', function($data, $file, $filename, $mimes) {
global $wp_version;
if ( $wp_version !== '4.7.1' ) {
return $data;
}
$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' );
Easily add code to your functions.php file with the plugin CODE SNIPPETS
Because it is easy, I prefer to use the WordPress plugin CODE SNIPPETS to organize and add personalized code updates to my WordPress site’s functions.php file.
The same Enable SVG PHP code is shown below in a screenshot. The screenshot is SVG enabling code applied to a WordPress site via the CODE SNIPPETS plugin.
I hope this blog post has successfully helped you fix the “Sorry, this file type is not permitted for security reasons.” warning in WordPress.