SVG is the best format for logos and icons: it stays sharp at any size and the files are tiny. But if you try to upload one to WordPress, you get this:
Sorry, you are not allowed to upload this file type.
This post gives you a small snippet that fixes that, without opening a security hole.
Why WordPress blocks SVG
An SVG is not a normal image. It is an XML file, and XML can carry JavaScript. A bad SVG can run code in the browser of anyone who opens it, including an admin in the dashboard. That is why WordPress leaves SVG out of its allowed file types.
So the rule is simple: only let people you trust upload SVG files. The snippet below allows SVG for administrators only. Authors, editors and other roles still get the normal error.
The code
/**
* Allow SVG uploads for administrators only
*/
add_filter( 'upload_mimes', function ( $mimes ) {
if ( current_user_can( 'manage_options' ) ) {
$mimes['svg'] = 'image/svg+xml';
}
return $mimes;
} );
/**
* Fix "Sorry, you are not allowed to upload this file type" for SVG.
* Some servers report SVG as text/plain or image/svg, so the real-type check fails.
*/
add_filter( 'wp_check_filetype_and_ext', function ( $data, $file, $filename, $mimes ) {
if ( ! current_user_can( 'manage_options' ) ) {
return $data;
}
$filetype = wp_check_filetype( $filename, $mimes );
if ( 'svg' === $filetype['ext'] ) {
$data['ext'] = 'svg';
$data['type'] = 'image/svg+xml';
}
return $data;
}, 10, 4 );
The first filter adds SVG to the list of allowed types. The second one helps on servers that detect SVG with the wrong type. Without it, the upload can still fail even after the first filter.
Show SVG previews in the Media Library
SVG files have no fixed size, so WordPress sometimes shows them as an empty box in the Media Library. This small piece of CSS fixes the preview:
/**
* Show SVG thumbnails in the Media Library
*/
add_action( 'admin_head', function () {
echo '<style>
.media-modal img[src$=".svg"],
.wp-list-table img[src$=".svg"] {
width: 100% !important;
height: auto !important;
}
</style>';
} );
Optional: allow web fonts too
Need to upload your own font files, for example for Elementor custom fonts? Add them to the same filter:
/**
* Allow SVG and web fonts for administrators only
*/
add_filter( 'upload_mimes', function ( $mimes ) {
if ( current_user_can( 'manage_options' ) ) {
$mimes['svg'] = 'image/svg+xml';
$mimes['woff'] = 'font/woff';
$mimes['woff2'] = 'font/woff2';
$mimes['ttf'] = 'font/ttf';
}
return $mimes;
} );
Use this one instead of the first filter above, not as well as it. One upload_mimes filter is enough.
How to add the code
- Open your child theme’s
functions.php, or install a snippets plugin such as Code Snippets and create a new PHP snippet. Never edit a parent theme: an update will wipe your changes. - Paste the code and save. If you use Code Snippets, set it to run in the admin area or everywhere.
- Log in as an administrator and go to Media → Add New.
- Upload a test SVG. It should upload and show a preview.
Keep it safe
- Only upload SVG files you trust. Logos from your designer or icons from a known library are fine. Random files from the internet are not.
- Clean the file first. Tools like SVGOMG remove scripts and junk, and make the file smaller too.
- Need SVG for more roles? Use a plugin such as Safe SVG. It sanitizes every SVG on upload, which a code snippet does not do.
That is it. You get sharp logos and icons, and only admins can upload them.

