Enable SVG in WordPress Media Library using a function

E

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.”

WordPress warning: "Sorry, this file type is not permitted for security reasons."
WordPress warning: “Sorry, this file type is not permitted for security reasons,” or “Sorry, you are not allowed to upload this file type.”

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.

PHP code placed in CODE SNIPPETS that updates the function.php to permit the upload of SVG files

I hope this blog post has successfully helped you fix the “Sorry, this file type is not permitted for security reasons.” warning in WordPress.

Happy Blogging!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories